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

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.