Results 1 to 15 of 15
  1. #1
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    3

    Default Mad null pointer, but i dont see why this is!

    Basically when i run my program i get a null pointer (Grr...)

    i am calling a method that creates an array in a different class, but im am calling it when i create the main program.

    NULL POINTER OCCURS, IN INVENTORY AND THE METHOD CALL AR.CREATEARRAY();

    error message:

    java.lang.NullPointerException
    at Inventory.<init>(Inventory.java:32)
    at Menu.<init>(Menu.java:25)
    at GoodArt.main(GoodArt.java:7)


    Java Code:
    public class Inventory 
    {
        private Art a; //holds the Art class and allows piece collection
        private Artist ar;
        private String name;
        private TheSimpleFrame tsf;
        private Scanner scan; //allows user input
        private int total; //total value of the collection
        private List<Art> collection;
        private List<Artist> artists;
        private int nextFreeLocation; //used to determine size
        private int capacity, capacity2;
        private double sessionTotal; //allows me to keep a running total for session
        private double artistTotal;//allows me to keep a running total for artist
        private double galleryTotal;//allows me to keep a running total for gallery
        private double VATTotal; //allows me to keep a running total for VAT
        public final double VAT=0.2; //public variable for VAT
    
    public Inventory(int maxCollectionSize, int maxNoArtists)
        {
            nextFreeLocation=0; //empty array so next free location is 0
            capacity=maxCollectionSize;
            capacity2=maxNoArtists;
            capacity3=maxNoPieces;
            tsf=new TheSimpleFrame();
            scan=new Scanner(System.in);
            collection=new ArrayList<Art>(capacity); 
            artists=new ArrayList<Artist>(capacity2);
            ar.createArray();
        }
    }
    Java Code:
     public void createArray(){
            pieces=new ArrayList<Art>();
        }
    Java Code:
    public Menu()
        {
            System.out.println("constructor for Menu");
            scan=new Scanner(System.in);
            System.out.print("Please enter the file name of Inventory information: ");
            filename=scan.next();
            System.out.print("enter the max amount of pieces in collection: ");
            int max=scan.nextInt();scan.nextLine();
            System.out.print("enter the max amount of Arists in Gallery: ");
            int max2=scan.nextInt();scan.nextLine();
            System.out.print("enter the max amount of peices artists can have:");
            int max3=scan.nextInt();scan.nextLine();
            inv=new Inventory(max, max2, max3);
    Last edited by monkeyjr97; 11-30-2012 at 03:11 AM.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Mad null pointer, but i dont see why this is!

    The NullPointerException is thrown when you trying to access a null when an object is required. In your case the "ar" is only declared, you've never create any object for it. That mean "ar" == null. And then you try to access the createArray() method from it. So it will give you a NullPointerException. Before you can access any methods from an object you have to instantiate it.

    Java Code:
    Artist ar = new Artist();
    ar.createArray();

  3. #3
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    3

    Default Re: Mad null pointer, but i dont see why this is!

    cheers mate! stupidly overlooked this! i thought it was due to the method being off!

    solved cheers!

    rep

  4. #4
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    3

    Default Re: Mad null pointer, but i dont see why this is!

    ARGH! new null pointer now!


    java.lang.NullPointerException
    at Artist.addPiece(Artist.java:159)
    at Inventory.addArt(Inventory.java:96)
    at Menu.UserMenu(Menu.java:61)
    at GoodArt.main(GoodArt.java:9)


    pretty much same code apart from

    Java Code:
    Artist ar = new Artist();
    ar.createArray();
    has been added to inventory constructor

  5. #5
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Mad null pointer, but i dont see why this is!

    The rule is the same. Please check if this object has been initialize before you can access it. If you don't know whether the reference you are accessing can be null or not check it using the if statement.

    Java Code:
    if (artist != null) {
        artist.doSomething();
    } else {
       System.out.println("Artist is null!");
    }

  6. #6
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    3

    Default Re: Mad null pointer, but i dont see why this is!

    this is the code i have to call the method. both artist and art are defined.

    Java Code:
    public void addArt() 
        {
            Art a=new Art();
            System.out.println("enter name of artist \n");
            String artistname=scan.nextLine();
            System.out.print("enter title of art piece \n");
            String title=scan.nextLine();
            System.out.print("enter value of art piece \n");
            int value=scan.nextInt();scan.nextLine();
            System.out.print("enter filename of art piece INCLUDE EXTENSION! \n");
            String file=scan.nextLine();
            Artist who=this.searchForWho(artistname);
            if (who==null) {
                System.out.println("No artist found. creating new one");
                Artist who2=new Artist();
                who2=this.addArtist2(who);
                a=new Art(artistname, title, value, file, who2);
                who2.addPiece(a);
                collection.add(a);                      
            }else {
                a=new Art(artistname, title, value, file, who);
                who.addPiece(a);
                collection.add(a);
            }
        }

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Mad null pointer, but i dont see why this is!

    When you run into a NPE, before posting it here, now that you know how to look for it, find the variable that's null and look back into your code to see why. The lines that the NPE mentions will tell you where to look.

  8. #8
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    3

    Default Re: Mad null pointer, but i dont see why this is!

    both Art(a) and Artist both seem to be defined and if it has a null value it creates one, ive traced my code numerous times but i cant figure it out :/

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Mad null pointer, but i dont see why this is!

    Show the StackTrace again, and indicate with comments in your posted code which lines are throwing the exception.

    Also, this line looks very suspect:
    Java Code:
    who2=this.addArtist2(who);

  10. #10
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    3

    Default Re: Mad null pointer, but i dont see why this is!

    lines of code that throw the expection based off:
    java.lang.NullPointerException
    at Artist.addPiece(Artist.java:159)
    at Inventory.addArt(Inventory.java:96)
    at Menu.UserMenu(Menu.java:61)
    at GoodArt.main(GoodArt.java:9)

    are

    Java Code:
    public void addPiece(Art a){
            pieces.add(a); //NPE
        }
    Java Code:
    public void addArt() 
        {
            Art a=new Art();
            System.out.println("enter name of artist \n");
            String artistname=scan.nextLine();
            System.out.print("enter title of art piece \n");
            String title=scan.nextLine();
            System.out.print("enter value of art piece \n");
            int value=scan.nextInt();scan.nextLine();
            System.out.print("enter filename of art piece INCLUDE EXTENSION! \n");
            String file=scan.nextLine();
            Artist who=this.searchForWho(artistname);
            if (who==null) {
                System.out.println("No artist found. creating new one");
                Artist who2=new Artist();
                who2=this.addArtist2(who);
                a=new Art(artistname, title, value, file, who2);
                who2.addPiece(a); // THIS LINE
                collection.add(a);                      
            }else {
                a=new Art(artistname, title, value, file, who);
                who.addPiece(a);
                collection.add(a);
            }
    in summary lines:

    2 of the 1st method

    line 18 of second method

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Mad null pointer, but i dont see why this is!

    Again, this looks wicked bad:
    Java Code:
    Artist who2=new Artist();
    who2=this.addArtist2(who);
    Just what are you trying to do with that second line, and what does the addArtist2(...) method look like and return. In fact it should be obvious to you that it's returning null, and that's why you get the NPE when you try to use the who2 variable. Get into your code and trace back.

    but more importantly, learn these general concepts yourself. You know which variable is null by the NPE, now trace back into your code a little more aggressively.

  12. #12
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    3

    Default Re: Mad null pointer, but i dont see why this is!

    that second line basically says if there isnt an artist with said name in array list create one using:

    Java Code:
    ublic Artist addArtist2(Artist newartist) 
        {
            newartist=new Artist();
            System.out.println("enter name of artist \n");
            String n=scan.nextLine();
            System.out.print("enter address of Artist \n");
            String ad=scan.nextLine();
            System.out.print("Enter email address of Artist \n");
            String em=scan.nextLine();
            System.out.print("Enter contact number for Artist \n");
            String tel=scan.nextLine();
    
            //finished constructing dog(x, x, x, x);
            newartist=new Artist(n, ad, em, tel);
            return newartist;
    
        }
    is it something to do with the fact that i am returning the value or newartist?

    or should i pass a string into the method instead of an Artist, pretty confused, tried a few things to no avail
    Last edited by monkeyjr97; 11-30-2012 at 04:28 AM.

  13. #13
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Mad null pointer, but i dont see why this is!

    Quote Originally Posted by monkeyjr97 View Post
    Java Code:
    public void addPiece(Art a){
            pieces.add(a); //NPE
        }
    You mark that line that throw the NPE. Then make sure the the "pieces" is not null.

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Mad null pointer, but i dont see why this is!

    You shouldn't pass anything into this message since you do nothing with the value passed in (it's null after all).

  15. #15
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    3

    Default Re: Mad null pointer, but i dont see why this is!

    ive tried loads of changes! just being a complete idiot here i think dont see why pieces.add(a) is a null pointer.
    i defined pieces in both Artist, and i created it when i created the inventory. surely if it was to do with the who2 and stuff then wouldnt the NPE be on line who2.addPiece(a);

Similar Threads

  1. null pointer exception
    By bequick01 in forum New To Java
    Replies: 3
    Last Post: 04-28-2011, 09:31 PM
  2. Null Pointer
    By theen3my in forum AWT / Swing
    Replies: 3
    Last Post: 10-03-2009, 03:10 PM
  3. Null pointer exception
    By talha06 in forum JDBC
    Replies: 5
    Last Post: 07-14-2009, 02:12 AM
  4. null pointer exception
    By anthonym2121 in forum New To Java
    Replies: 7
    Last Post: 04-06-2009, 04:25 AM
  5. null pointer help
    By mayhewj7 in forum New To Java
    Replies: 5
    Last Post: 02-18-2009, 12:51 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
  •