James
James

Reputation: 107

Why is there an NullPointerException?

I am trying to mommertary use a string and convert it to an int to compare the first first column and all the rows with the all of the numbers within the string typed in. When I type in a number, I get a NullPointerException. The thing is, I don't understand why the compiler is telling me this when I feel like I have declared all my objects properly. please help!

import java.util.ArrayList;

public class Decoder
{

    private int[][] zipdecoder;
    private ArrayList<Integer> zipcode;
    private String finalCode;
    private String bars;
    private int place;
public Decoder()
{
   int[][] zipdecoder = new int[][]{
       {1,0,0,0,1,1},
       {2,0,0,1,0,1},
       {3,0,0,1,1,1},
       {4,0,1,0,0,0},
       {5,0,1,0,1,1},
       {6,0,1,1,0,0},
       {7,1,0,0,0,0},
       {8,1,0,0,1,1},
       {9,1,0,1,0,0},
       {0,1,1,0,0,0}
       };
    zipcode = new ArrayList<Integer>();
}

public void insertToArray(String zip)
{
    int count = 0;

    for(int i = 1; i<zip.length()+1;i++)
    {
        String piece = zip.substring(count, i);

        int number = Integer.parseInt(piece);
        for(int j = 0;j<10;j++)
        {
            if(number == zipdecoder[j][0]){
            for(int a = 1;a<5;a++)
            {
                zipcode.add(place,zipdecoder[j][a]);
                place++;
            }
        }
        count++;
    }

    }
}

Upvotes: 1

Views: 96

Answers (1)

Andreas Dolk
Andreas Dolk

Reputation: 114767

You're not initializing the class member zipdecoder but a new local variable (with the same name) in the constructor.

Change this

 int[][] zipdecoder = new int[][]{

to

 zipdecoder = new int[][]{

and it should work.

Upvotes: 10

Related Questions