Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Utterly Stuck on a NullPointerException From Nowhere!

  1. #1
    ssrock64 is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Angry Utterly Stuck on a NullPointerException From Nowhere!

    Hello, all. I'm working on a computer program for AP Computer Science, which I'm taking online. I'm late on this program already, and have been hung up by the dearth of my programming existence, a NullPointerException. The thing is, I can't figure out what's doing it this time. Here's the three classes involved:

    Java Code:
    import java.util.*;
    public class Bin
    {
      
      
      
      private String myName;
      
      private ArrayList<BinItem> myContents;
      
      
      
      public Bin(String name)
      {
        
        myName = name;
        
      }
      
      
      
      public String getName()
      {
        
        return myName;
        
      }
      
      public ArrayList<BinItem> getContents()
      {
        
        return myContents;
        
      }
      
      public void add(BinItem b)
      {
        
        myContents.add(b);
        
      }
      
      public String toString()
      {
        
        String s = "Bin " + myName + ":/n";
        
        for(BinItem b : myContents)
          s += b + "\n";
        
        return s;
        
      }
    }

    Java Code:
    public class BinItem 
    { 
      
      
      
      private String mySKU; 
      private int myQuantity; 
      
      
      
      public BinItem( String sku, int quantity ) 
      { 
        
        mySKU = sku; 
        
        myQuantity = quantity; 
        
      } 
      
      
      
      public String getSKU()  
      {  
        
        return mySKU; 
        
      } 
      
      
      
      public int getQuantity() 
      { 
        
        return myQuantity; 
        
      } 
      
      
      
      public String toString() 
      { 
        
        return "SKU " + getSKU() + ": " + getQuantity();
        
      } 
    }


    Java Code:
    import java.util.*;
    public class Project3Runner
    {
      
      
      
      public static ArrayList<MusicMedia> MakeMusicCatalog( int size ) 
      { 
        
        String[] titles =  
        { 
          
          "Greatest Hits Volume 1", "Greatest Hits Volume 2", 
          "The Best Of", "Love Songs", 
          "The Early Years", "The Later Years" 
          
        }; 
        
        String[] artists =  
        { 
          
          "Michael Jackson", "Eminem", 
          "The Beatles", "Shania Twain", 
          "Limp Bizkit" 
          
        }; 
        
        ArrayList<MusicMedia> a = new ArrayList<MusicMedia>(); 
        
        Random rn = new Random(); 
        
        for ( int i = 0 ; i < size ; i++ ) 
        { 
          
          MusicMedia m; 
          
          int mediatype = rn.nextInt( 3 ); 
          
          String title = titles[ rn.nextInt( titles.length ) ]; 
          
          String artist = artists[ rn.nextInt( artists.length ) ]; 
          
          String sku = "1234-" + i; 
          
          if ( mediatype == 0 ) 
            m = new CompactDisk( title, artist, sku ); 
          
          else if ( mediatype == 1 ) 
            m = new DigitalVideoDisk( title, artist, sku ); 
          
          else 
            m = new CassetteTape( title, artist, sku ); 
          
          a.add( m ); 
          
        } 
        
        return a; 
        
      }
      
      public static void main( String[] args ) 
      { 
        
        ArrayList<Bin> warehouse = new ArrayList<Bin>();
        
        Bin a = new Bin( "A" );
        
        Bin b = new Bin( "B" );
        
        warehouse.add( a );
        
        warehouse.add( b );
        
        a.add( new BinItem( "1234-0", 500 ) );
        
        a.add( new BinItem( "1234-1", 25 ) );
        
        a.add( new BinItem( "1234-2", 7720 ) );
        
        b.add( new BinItem( "1234-3", 1000 ) );
        
        for ( Bin bn : warehouse )
          System.out.println( bn );
      }
    }
    The exception traces to where line 75 in Project3Runner utilizes line 39 of Bin.

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

  3. #3
    ssrock64 is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: Utterly Stuck on a NullPointerException From Nowhere!

    In Project3Runner, it's "a.add( new BinItem( "1234-0", 500 ) );"

    In Bin, it's "myContents.add(b);"

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

    Default Re: Utterly Stuck on a NullPointerException From Nowhere!

    The error is not "coming out of nowhere". The object on the first line noted in the exception's stacktrace, you're not initializing it. You simply need to initialize it for this to work. The key here is to carefully read the exception message, believe what it says, and act on it. That's all that needs to be done. So where do you initialize myContents?

  5. #5
    ssrock64 is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: Utterly Stuck on a NullPointerException From Nowhere!

    I always do something incredibly, basically dumb and think I've done something more nuanced. Thanks, and sorry for wasting the internet!

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

    Default Re: Utterly Stuck on a NullPointerException From Nowhere!

    Nothing dumb about it if you take the lesson to heart: respect and believe your error messages and exceptions.
    quad64bit likes this.

Similar Threads

  1. I'm new and stuck, please help :(
    By XoVoX in forum Advanced Java
    Replies: 6
    Last Post: 07-15-2012, 07:48 PM
  2. Stuck, need help please
    By Johnny2009 in forum New To Java
    Replies: 3
    Last Post: 11-03-2011, 10:25 PM
  3. I'm stuck help!!!
    By nobody58 in forum Advanced Java
    Replies: 2
    Last Post: 03-18-2010, 02:52 PM
  4. Im on my last lab!!!! And im stuck...:(
    By clanboru15 in forum New To Java
    Replies: 5
    Last Post: 03-13-2009, 01:44 AM
  5. Stuck in need of help!
    By Zombie_Leg! in forum New To Java
    Replies: 1
    Last Post: 09-23-2008, 02:22 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
  •