Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Value is null even after i have allocated memory for the object

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Value is null even after i have allocated memory for the object

    Hi,
    I am a total newbie in java , and i am facing this issue. after allocating memory for my class instance, i am not able to access the variable in this instance.
    here is the code for this.

    Main function:
      String ip=args[0];
                FileReader input = new FileReader(ip);
                BufferedReader bufRead = new BufferedReader(input);
                String line;
                int count = 0;
     
                line = bufRead.readLine();      // read first line
                int v = Integer.parseInt(line);
                //System.out.print("Value of number is "+ v);
                count++;
                g1=g1.createGraph(v,g1);
    CreateGraph:
    Graph createGraph(int v,Graph g1){
            g1.numv=v;
            g1.g=new Vertex[v];
        //    g1.g[0].id=4;
            System.out.println("value of g[0].id is"+g1.g[0].id );
            for (int i=0;i<v;i++)
            {
                g1.g[i]=new Vertex();
                g1.g[i].id=i;
            }
            return g1;
    The output i am getting is :

    Exception in thread "main" java.lang.NullPointerException
    at graphcolouring.Graph.createGraph(Graph.java:21)
    at graphcolouring.GraphColouring.main(GraphColouring. java:29)
    Last edited by helloworld922; October 26th, 2012 at 10:09 AM. Reason: please use [code] tags


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Value is null even after i have allocated memory for the object

    What variable is null in that line?

    Also, declaring a variable does not initialize it. Until you initialize it, the variable's value is null.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Properly releasing memory to avoid memory pileup/crash
    By fickletrick in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 22nd, 2012, 10:09 AM
  2. [SOLVED] Memory usage increasing in while loop - is it a memory leak
    By mds1256 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 18th, 2012, 10:06 AM
  3. DataSource null pointer exception from object
    By jyounger in forum JDBC & Databases
    Replies: 0
    Last Post: February 20th, 2012, 05:44 AM
  4. Null or not an object error
    By James W in forum Other Programming Languages
    Replies: 4
    Last Post: November 1st, 2011, 01:06 PM
  5. How do you set an object in array to null value?
    By Arius in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 25th, 2010, 03:50 AM