Skip to main content

arraylist java compiling errors



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


Comments

  1. 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>();
    }

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

    ReplyDelete
  3. 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));
    }

    ReplyDelete
  4. 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>();.

    ReplyDelete
  5. 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.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex