Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...