0

can anyone find me the mistake in the following code?

This is the multiplication of matrices that contain fractions. It shows me a NullPointerException and underscores the line with z[i][j]. The scitaj and vynasob methods are already tested and the error should not be there. I am attaching a picture. If you need to see a larger part of the code for understanding, tell me.

Thank you very much :)

matica - matrix zlomok - fraction nasobenie, vynasob - multiplication PocetRiadkov - number of rows PocetStlpcov - number of columns scitaj - +

public Matica nasobenie() {
    Matica xs = new Matica(new Zlomok[][]
    {
        {new Zlomok(1, 1), new Zlomok(2, 1), new Zlomok(3, 1)},
        {new Zlomok(4, 1), new Zlomok(5, 1), new Zlomok(6, 1)}
    });
        
    Matica ys = new Matica(new Zlomok[][]
        {
            {new Zlomok(7, 1), new Zlomok(8, 1)},
            {new Zlomok(9, 1), new Zlomok(10, 1)},
            {new Zlomok(11, 1), new Zlomok(12, 1)}
        });
    
    Zlomok [][] z = new Zlomok[xs.getPocetRiadkov()][ys.getPocetStlpcov()];
    System.out.println(z);
    for (int i = 0; i < z.length; i++) {
        for (int j = 0; j < z[0].length; j++) {
            for (int k = 0; k < xs.getPocetStlpcov(); k++) {
                z[i][j] = z[i][j].scitaj(xs.getPrvok(i, k).vynasob(ys.getPrvok(k, j)));
            }
            System.out.println();
        }
    }
    Matica vysledok = new Matica(z);
    return vysledok;        
}
7
  • 1
    Please, don't put code as images. The line is throwing a NPE because there's nothing at 0,0 for the matrix. The first value is in 1,1 Commented Nov 22, 2022 at 12:57
  • 1
    Hi and welcome. Can you please relevant code into the question, please? No external links. Commented Nov 22, 2022 at 12:58
  • z[i][j] is null, not a value representing zero, you cannot call scitaj on it until a proper value has been assigned Commented Nov 22, 2022 at 13:50
  • @LeonardoEmmanueldeAzevedo I'm not clear why you'd expect there to be a value at 1,1? Commented Nov 22, 2022 at 13:53
  • That's what your code says, the first value is at 1,1 and not at 0,0. The for is starting from 0 Commented Nov 22, 2022 at 14:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.