Mitesh Khadgi
Mitesh Khadgi

Reputation: 25

Nullpointer exception error for using split and comma operators

I want to store the values HTRANS and HBURST in one list and 2'b00 and 3'b000 in another list. But i am getting a null pointer exception in the code which i have written, I have written an SSCCE for the same, please help.

import java.util.ArrayList;
import java.util.List;

public class SSCCE1 {
    public static void main(String[] args) {
        List<String> bins = new ArrayList<String>();
        bins.add("HTRANS = 2'b00 to 2'b11");
        bins.add("HTRANS = 2'b00 to 2'b11, HBURST = 3'b000 to 3'b111");
        String[] bins_splitcomma = null;
        String temp;
        for(int i =0; i < bins.size(); i++) {
            temp = bins.get(i);
            if(temp.contains(",")) {                // if element of bins contains comma, case 2.
                bins_splitcomma = temp.split(",");
            }
            else {
                bins_splitcomma[0] = temp;          // if element of bins does not contain a comma, case 1.
            }

        }
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException
    at codecoveragetool.SSCCE1.main(SSCCE1.java:28)
Java Result: 1

My full code:

String temp1;
                String[] bins_splitcomma = null;
                String[] bins_split;
                List<String> bins_name = new ArrayList<String>();
                List<String> bins_bitrange = new ArrayList<String>();
                //List<String> bins_bits = new ArrayList<String>();
                ArrayList<ArrayList<String>> bins_bits = new ArrayList<ArrayList<String>>();
                List<String> bins = dataStr.get("bins");
                System.out.println(bins);
                for(int i =0; i < bins.size(); i++) {
                    temp1 = bins.get(i);
                    if(temp1.contains(",")) {
                        bins_splitcomma = temp1.split(",");
                    }
                    else {
                        bins_splitcomma = new String[]{temp1};
                    }
                    for(int j = 0; j < bins_splitcomma.length; j++) {
                        bins_split = bins_splitcomma[j].split("=");                         // HBURST = 3'b000 to 3'b111
                        if(!(bins_name.contains(bins_split[0].trim()))) {
                            bins_name.add(bins_split[0].trim());                        // HBURST
                            bins_bitrange.add(bins_split[1].trim());                    // 3'b000 to 3'b111
                            ArrayList<String> tempo = returnBits(bins_split[1].trim()); // [3'b000, 3'b001, 3'b010, 3'b011, ... , 3'b111]
                            bins_bits.add(tempo);
                        }
                    }
                }

Upvotes: 0

Views: 223

Answers (4)

Patricia Shanahan
Patricia Shanahan

Reputation: 26185

In the else path you need to create an array, not try to access element 0 of a null array reference.

bins_splitcomma = new String[]{temp};

Here is my complete test program:

import java.util.ArrayList;
import java.util.List;

public class Test {
  public static void main(String[] args) {
    List<String> bins = new ArrayList<String>();
    bins.add("HTRANS = 2'b00 to 2'b11");
    bins.add("HTRANS = 2'b00 to 2'b11, HBURST = 3'b000 to 3'b111");
    String[] bins_splitcomma = null;
    String temp;
    for(int i =0; i < bins.size(); i++) {
        temp = bins.get(i);
        if(temp.contains(",")) {                // if element of bins contains comma, case 2.
            bins_splitcomma = temp.split(",");
        }
        else {
            bins_splitcomma = new String[]{temp};          // if element of bins does not contain a comma, case 1.
        }
    }
  }

}

Upvotes: 2

Paul Kienitz
Paul Kienitz

Reputation: 878

In the else-part, you're assigning to element 0 of a null array reference. Need to allocate an array of one string.

Upvotes: 0

Naman Gala
Naman Gala

Reputation: 4692

As you first element does not contain ,, so your code will go in else statement which will try to put value at 0th position of bins_splitcomma. But you never initialized it. Try this code

...
else {
    if (bins_splitcomma == null) {
        bins_splitcomma = new String[5];
    }
    bins_splitcomma[0] = temp;          // if element of bins does not contain a comma, case 1.
}

Upvotes: 0

NickJ
NickJ

Reputation: 9559

Your line

bins_splitcomma[0] = temp;

is trying to set an element of a null array as defined in your line

String[] bins_splitcomma = null;

Upvotes: 3

Related Questions