0

ok i try to play with arraylist in java awhile to experiment some thing that related my project..so i come up with a simple code like this

having 3 file...DataStruc.java , DataStrucHand.java , testcase1.java

DataStruc.java


public class DataStruc {
private String testString;

public DataStruc(String s){
    this.testString = s;
}

public String getTestString() {
    return testString;
}

public void setTestString(String testString) {
    this.testString = testString;
}

public String toString(){
    return String.format("%s",testString);
}
}


DataStrucHand.java

 import java.util.ArrayList;

  public class DataStrucHand {
private ArrayList<DataStruc> ds;

public void addData(String ss){
    ds.add(new DataStruc(ss));
}

public ArrayList<DataStruc> getData(){
    return ds;
}
}


testcase1.java
 import java.util.*; 
 public class testcase1 {
public static void main(String args []){
    DataStrucHand dsh = new DataStrucHand();

    String gdata = "test";

    dsh.addData(gdata);


  }
   }

i tried to compile it and having this error

 Exception in thread "main" java.lang.NullPointerException
at DataStrucHand.addData(DataStrucHand.java:7)
at testcase1.main(testcase1.java:8)

can i know what is wrong actually? i cant even add the data...i am trying to add the data and retrieve it back by creating another testcase2.java...but than i having problems in adding now to the arraylist...my purpose is to create a temp storage to keep a specific string that can be obtain by 1 program but runs with 2 different classes..

0

5 Answers 5

2

You never assign anything to the ds field.

DataStrucHand.java

import java.util.ArrayList;

public class DataStrucHand {
    private ArrayList<DataStruc> ds; //I am null because nothing is ever new'd up here...

    public void addData(String ss){
        ds.add(new DataStruc(ss));
    }

    public ArrayList<DataStruc> getData(){
        return ds;
    }
}

Try it with this line:

private ArrayList<DataStruc> ds = new ArrayList<DataStruc>();

Or, you can have a constructor that will new it up if you prefer that method:

public DataStrucHand() {
     ds = new ArrayList<DataStruc>();
}
Sign up to request clarification or add additional context in comments.

2 Comments

oh my god, thanks for the help...is like 3.50AM here and i really need some sleep
@user1217609: You need sleep and to learn the steps on how to debug a NPE. The key is to check the variables on the line throwing the exception then trace back in the code to see why it's null.
0

You need to put an ArrayList<DataStruc> instance in ds.

Comments

0

You haven't instantiated the ArrayList. Write this:

public class DataStrucHand {
    private ArrayList<DataStruc> ds = new ArrayList<DataStruc>();

    public void addData(String ss){
       ds.add(new DataStruc(ss));
    }

Comments

0

You've never initialized ds, so it is null when you call ds.add(new DataStruc(ss)); Add a constructor to DataStrucHand that initializes ds, such as ds = new ArrayList<DataStruc>();.

Comments

0

The problem is that your DataStrucHand class never initializes its private field ds, so when you try to call ds.add(...) it fails with NullPointerException.

In fact, the way the class looks right now, there is no way ds can be anything else than null.

Shortest way to fix this is to initialize ds properly:

private final List<DataStruc> ds = new ArrayList<DataStruc>();

This way each DataStrucHand instance is constructed with an ArrayList inside and ds is never null.

Comments

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.