Results 1 to 3 of 3
  1. #1
    javagirl24 is offline Banned
    Join Date
    May 2012
    Posts
    1
    Rep Power
    0

    Default Exception in thread "main" java.lang.NullPointerException

    I am getting this exception in the code below for not initializing arraylist.. could someone please help me with this..:(
    this problem is in g[i].v.add(vv);


    class graph
    {

    public ArrayList<Integer[]> v=new ArrayList<Integer[]>();
    public ArrayList<Integer[]> e=new ArrayList<Integer[]>();


    }


    public class main {



    public static void main(String[] args) {

    Integer vv[]=new Integer[2];
    Integer ee[]=new Integer[3];
    graph g[]=new graph[1000];
    int i=-1;
    String t="t";
    String v="v";
    String e="e";
    FileInputStream fstream;
    try {
    fstream = new FileInputStream("graph1.txt");


    DataInputStream in = new DataInputStream(fstream);
    BufferedReader b = new BufferedReader(new InputStreamReader(in));

    String line;

    try {
    while ((line = b.readLine()) != null) {
    StringTokenizer st = new StringTokenizer(line);

    if(t.equals(st.nextToken()))
    {
    i++;

    }
    line = b.readLine();
    System.out.println(line);
    StringTokenizer st1 = new StringTokenizer(line);

    if(v.equals(st1.nextToken()))
    {

    vv[0]=Integer.parseInt(st1.nextToken());
    vv[1]=Integer.parseInt(st1.nextToken());
    g[i].v.add(vv);
    }
    line = b.readLine();
    StringTokenizer st2 = new StringTokenizer(line);

    if(e.equals(st2.nextToken()))
    {
    ee[0]=Integer.parseInt(st2.nextToken());
    ee[1]=Integer.parseInt(st2.nextToken());
    ee[2]=Integer.parseInt(st2.nextToken());
    g[i].e.add(ee);

    }


    }
    } catch (IOException ex) {
    Logger.getLogger(main.class.getName()).log(Level.S EVERE, null, ex);
    }
    } catch (FileNotFoundException ex) {
    Logger.getLogger(main.class.getName()).log(Level.S EVERE, null, ex);
    }

    }}

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: Exception in thread "main" java.lang.NullPointerException

    What variable has a null value?

    Please post the full text of the error message.


    Some bad practices I see are:
    using variable names that are only 1 character long
    having more than one variable with the same name: e & v

    You need to fix those.

    Also use code tags: http://www.java-forums.org/misc.php?do=bbcode#code
    Last edited by Norm; 05-10-2012 at 09:31 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Duplicate of Exception in thread "main" java.lang.NullPointerException

    Closing the thread and banning the javagirl24 account.

    db

    THREAD CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 6
    Last Post: 04-19-2012, 07:10 PM
  2. Replies: 5
    Last Post: 12-21-2011, 06:44 PM
  3. Replies: 6
    Last Post: 07-16-2009, 03:30 PM
  4. Replies: 1
    Last Post: 07-16-2009, 10:35 AM
  5. Replies: 1
    Last Post: 07-16-2007, 06:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •