Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

java - variable read in main folder not test folder

I'm new to java. I'm facing a strange problem. Here I have 2 folders in the project for main and for test [![enter image description here][1]][1]

I defined public static variable in class in trident.functions, then used it in another class in entities by taking object form class, here everything is OK and variable is read well as it holds a number due to an equation I wrote, then stored result in this variable.

Problem with me in test folder. When I maven code I got failure with test classes when I debugged code I found that variable that I defined before hold zero not number, why this variable not read number, however it worked well before with other classes in main?

public class Vector implements Function {
    public static  double num;
    String words[]={"asd","wer","dfdf","rttyy"}
    public  Values getValues(Tweet tweet, String[] words){
    //this part of the code of the variable i defined 

        for(String w:words)
        {
            items.add(w);
        }
        num = items.size();
    }
     public Spar normVector(Spar vector) {
         double z = vector.getnum();
         vector = //some calculations on z ; 
         return vector;
     }
}

another class

public class Spar {
     public double getnum() {
         double x=Vector.num;
         return x;
     }
}

Test class

public class VectorTest { 
    Vector  vb;
    public VectorBuilderTest(String vbr) {
        super(vbr);
        vb = new VectorBuilder();
    }
    public void testNormalizeVector() {
        Spar sp = new Spar(values);
        Spar normalized = vb.normVector(sp);
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The code has several issues. It would not even compile.

  1. The local variable words in getValues is duplicated (compile error).
  2. The method normVector is declared to return Spar but it returns double (compile error).
  3. In the test class the field vb is not initialized i.e. null, so the line Spar normalized = vb.normVector(sp); would throw a NullPointerException (runtime error).
  4. The static variable num is set only in the method getValues, but it is not called from your test class at all.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...