2

I am writing a little code which is using threads to access the matrixes U, S and V from a matrix A using Jama and svd() method but I'm facing difficulties like Java Null pointer exception. My code is this:

public class SVD implements Runnable
    {
    private Thread t;
    public Matrix A;
    public int option;
    public Matrix result;

    public SVD(Matrix A, int optiune)
    {
        this.A = A;
        this.option = optiune;
    }

    @Override
    public void run()
    {
        if (option == 1)
            result = A.svd().getU();
        if(option ==2)
            result=A.svd().getS();
        if(option ==3)
            result=A.svd().getV();
    }

    public void start()
    {
        if (t == null)
        {
            t = new Thread(this);
            t.start();
        }

    }

}

Now when I try to create 3 SVD objects like this:

 Matrix A = Matrix.random(20, 20);
    SVD a1 = new SVD(A, 1);
    SVD a2 = new SVD(A, 2);
    SVD a3 = new SVD(A, 3);
    a1.start();
    a2.start();
    a3.start();
    Matrix U=a1.result;

The last line is the line which gets the error. What could cause the problem? It is something that I missed?

PS: Sorry for the formatting I don't know why it does that!

sparkhee93
  • 1,381
  • 3
  • 21
  • 30
Ra mi
  • 43
  • 6
  • 2
    `NullPointerException` is thrown when you call a method on a `null` Object. Try `System.out.println(a1);` before your erroneous line. – AMACB Jan 26 '16 at 19:39
  • It's printing algoritm.SVD@232204a1, so the object points to something! I think there is something with Matrix result but I don't get what seems to be the problem. – Ra mi Jan 26 '16 at 19:49
  • Hmm that's weird. Are you sure that the error is thrown on that line. – AMACB Jan 26 '16 at 19:53
  • Well infact i have a line which does some sort of processing with U that I wrote it and the error comes from there,but the problem comes from U, U is null I tested it, and I don't thing that Matrix U=a1.result; statement is erroneous! INFACT the problem comes somewhere from a1 thread. I know it's crazy that's what bothers me. Maybe it is something that I missed, but I don't seem get it! – Ra mi Jan 26 '16 at 20:01
  • What about `System.out.println(a1.result)`? Does that work? – AMACB Jan 26 '16 at 20:03
  • Exactly I tested now, confirmed it's null. – Ra mi Jan 26 '16 at 20:06

1 Answers1

0

result is from type Matrix, although your a1 is pointing to something, a1.result could be null, try call the run method before you do your last line, something like:

Matrix A = Matrix.random(20, 20);
SVD a1 = new SVD(A, 1);
SVD a2 = new SVD(A, 2);
SVD a3 = new SVD(A, 3);
a1.start();
a2.start();
a3.start();
a1.run();
Matrix U=a1.result;

Check the line a1.run(), that's in order to initialize the result field from a1.

Dazak
  • 1,011
  • 2
  • 9
  • 17
  • It's working! But why, the method run shouldn't be called implicitly by start()? – Ra mi Jan 26 '16 at 20:23
  • No, because in your start method you don't do a call to the run method, if that is what you want then you have to put a called of that method inside start – Dazak Jan 26 '16 at 20:27
  • I used your method but another problem occurs: I lost concurency, the code is not concurrent anymore! – Ra mi Jan 26 '16 at 20:58
  • Try putting t.run inside your start method – Dazak Jan 26 '16 at 21:06