-4

I have seen many answers with respect to the above error in Java, however most of them are telling what the error is, and I cannot find a way to correct it.

This is a snippet of my code in Java:

public static void optimalAlignmentScoreBU(String r, String s, int matchScore, int transition, int transversion, int indel) {

        int m = r.length();
        int n = s.length();
        Node[][] strg = new Node[m + 1][n + 1];

        // base cases
        strg[m][n].val = 0;
}

I get error at the line where I write strg[m][n].val =0; I have created a Node class as follows:

// ELEMENT OF DP MATRIX IS OF TYPE NODE
    public class Node {
        int val;
        ArrayList<Pair<Integer>> arrows = new ArrayList<Pair<Integer>>();
    }

// PAIR CLASS
public static class Pair<T> {
        T p1;
        T p2;

        public Pair(T p1, T p2) {
            this.p1 = p1;
            this.p2 = p2;
        }
    }

Can you tell me what is going wrong? Why is NULL being pointed? What can I do to correct this?

Saranya Gupta
  • 1,945
  • 2
  • 10
  • 14
  • 1
    `strg[m][n] ` is null – JCWasmx86 Oct 08 '20 at 14:58
  • 1
    `strg[m][n]` is null, so you can't access its `.val` field. Arrays are initialized with null values, you need to create a Node you'll put at that index, the node is what you'll be able to call `.val` on. – Aaron Oct 08 '20 at 14:58
  • 3
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – OH GOD SPIDERS Oct 08 '20 at 15:00

3 Answers3

2

You created a matrix of null Nodes.

    strg[m][n] = new Node();
    strg[m][n].val = 0; // Now there is a Node, no longer NPE.

Of course nicer is:

    Node node = new Node();
    node.val = 0;
    strg[m][n] = node;

where a new Node actually has its val field already 0.

Of course all matrix nodes must be created.

In java:

int[] v = new int[10]; // All 0.
boolean[] v = new boolean[10]; // All false.
String[] v = new String[10]; // All null.
double[] v = new double[10];  // All 0.0.
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • Hi! Thanks! So how should I create my matrix, like of what type? As right now I have created like -> Node[][] strg = new Node[m + 1][n + 1]; Should this be changed? – Saranya Gupta Oct 08 '20 at 15:12
  • That is fine, but then `for (int i = 0; i < m+1; ++i) for (int j = 0; j < n+1; ++j) strg[i][j] = new Node();`. Not nice. As `strg[i][j]` was null. – Joop Eggen Oct 08 '20 at 15:15
  • So this is a good method to initialize all the cells of my matrix? – Saranya Gupta Oct 08 '20 at 15:17
  • 1
    If you use all. Depends, sometimes one assigns new Node objects, and the old value was irrelevant. – Joop Eggen Oct 08 '20 at 15:18
1

strg is empty; all its values are null. You need to create a new Node.

When you created an array of non-primitives in java, it doesn't contain any actual objects yet--every item is null.

Paramecium13
  • 345
  • 1
  • 7
-1

NPE is when you call a methode on a null object ... use the debbuger and see where he is null

CLAIN Cyril
  • 123
  • 9