0

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);
                        }
                    }
                }
  • bins_splitcomma[0] = temp; fails beacuse bins_splitcomma is null. What do you want to to with this array? – flowit Oct 14 '15 at 10:13
  • i want to store HTRANS in one list and "2'b00 to 2'b11" in another list. and also HBURST in the first list and "3'b000 to 3'b111" to the second list. – Mitesh Khadgi Oct 14 '15 at 10:17
  • 1
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Erwan C. Oct 14 '15 at 10:17
  • i need a solution not a theory about null exception. – Mitesh Khadgi Oct 14 '15 at 10:22
  • You don't need to check for comma. If there's no comma, split will return a one-element array - exactly what you're trying to achieve. – Klitos Kyriacou Oct 14 '15 at 10:30
  • @MiteshKhadgi "i need a solution not a theory about null exception" - I beg to differ. The greater understanding of NullPointerExceptions you have, the less likely you are to encounter them in the future, and when you do, you'll understand exactly what causes them. – NickJ Oct 14 '15 at 10:44

4 Answers4

3

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;
NickJ
  • 9,380
  • 9
  • 51
  • 74
2

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.
        }
    }
  }

}
Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
0

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.
}
Naman Gala
  • 4,670
  • 1
  • 21
  • 55
0

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

Paul Kienitz
  • 878
  • 6
  • 25