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 5 of 5

Thread: No idea why I'm getting NullPointerException error

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy No idea why I'm getting NullPointerException error

    Hello everyone,

    I am brand new to this forum and I am a beginner at this Java malarkey!
    I am doing a project for college at the moment and am getting a NullPointerException error
    when I try to run my code and have no idea why I am getting it!
    Please can someone help me?
    I know my code is not the best and there are probably lots of things I am doing wrong (proper standards for how to write the code etc...)
    But for now I just need to figure out why I am getting the NullPointerException on my method.
    It is giving me the error on method setLabelHolder, which I am sure has access to the array pics[] and the int numItems...
    The array I am trying to populate - LabelHolder - I am sure has also been properly declared and initialised...
    Please see attached code, and apologies for the way it is in 2 separate files with main in 1 file and the rest of the code in the other, its that way on purpose for now, sorry if its confusing.

    THANKS!!!
    Attached Files Attached Files


  2. #2
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: No idea why I'm getting NullPointerException error

    Sorry guys, I made a few slight mistakes in the code I attached, I have corrected those errors here in this attachment, but still get the same error as before.
    Thanks.
    Attached Files Attached Files

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: No idea why I'm getting NullPointerException error

    Please copy the full text of the error message and paste it here.

    Also post the code being sure to wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: No idea why I'm getting NullPointerException error

    The problem is you have declared all your variables outside the methods (this is good as it means all the methods can access them) however inside the "set" methods eg. setPics you are declaring the variable again and this is making it's scope limited to that method. Instead of using:
    String pics[] = new String[numItems];
    Since it is already declared you just need to use:
    pics = new String[numItems];
    And this should clear up some of the NullPointerExceptions - you have done it a few times in your code.

    Quote Originally Posted by Norm View Post
    Please copy the full text of the error message and paste it here.

    Also post the code being sure to wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    This is the error he is getting:
    Exception in thread "main" java.lang.NullPointerException
            at Spellings.setLabelHolder(Spellings.java:216)
            at Spellings.<init>(Spellings.java:62)
            at SpellingsTest.main(SpellingsTest.java:14)

    However, the same mistake has been made a few times.


    EDIT: Okay I have fixed it for you:
    import java.io.*;
    import java.awt.*;
    import java.util.*;
    import java.applet.*;
    import javax.swing.*;
    import java.awt.event.*;
    import javax.swing.event.*; 
    import java.util.Scanner;
    import java.util.Random;
     
    public class Spellings extends JFrame 
    { 
     
       // get info from database, if ( age > 9 && number >= x ) give more levels.
     
      private JPanel panels[];
      private JPanel mainPanel;
      private JPanel panel1;
      private JPanel lastPanel;    
     
      private JLabel matchingDesc[];
      private JLabel labelHolder[];
     
      private ImageIcon icons[];
      private ImageIcon arrow, pic;
     
      private JButton next = new JButton();
     
      private JTextField textFields[];
      private int randoms[];
     
      private int count = 0;
     
      // fields to hold info passed to constructor:
     
      private String pics[]; // for group
      private String descrip[]; // for description
      private int letters[]; // for numLetters
      private int numQues; // for levels
      private int numItems; // for items
      private String img; // for image
     
      Scanner input = new Scanner( System.in );  
      Random random = new Random();
     
     
      public Spellings(final String group[], final int numLetters[], final String description[], int levels, String image, String title, int items) 
      {  
         super( title ); 
     
         setNumQues(levels);
         setNumItems(items);
         pics = new String[numItems];
         setPics(group);
     
         setDescrip(description);
         setLetters(numLetters);
         setImg(image);
     
         setArrow();
         setPic();
     
         setRandoms();
         setLabelHolder();
         setMatchingDesc();
         setTextFields(); 
         setIcons();  
     
         JPanel panels[] = new JPanel[numQues];
         JPanel mainPanel = new JPanel();
         JPanel panel1 = new JPanel();
         JPanel lastPanel = new JPanel(); 
     
         mainPanel.setPreferredSize(new Dimension(925, 1050));
         panel1.setPreferredSize(new Dimension(925, 125));
         panel1.add(new JLabel(pic));
         mainPanel.add(panel1);
     
         for ( int i = 0; i < numQues; i++ )
         {
            panels[i] = new JPanel();
            panels[i].setBackground(Color.lightGray);
            panels[i].setPreferredSize(new Dimension(925, 100)); 
            JLabel holder = new JLabel(icons[i]);
            JLabel holder2 = new JLabel(matchingDesc[i].getText());
            String holder3 = Integer.toString(letters[randoms[i]]);
            JLabel holder4 = new JLabel(holder3 + " letters"); 
            panels[i].add(holder2);
            panels[i].add(holder);
            panels[i].add(textFields[i]);
            panels[i].add(holder4); 
            mainPanel.add(panels[i]);
         } 
     
         lastPanel.setPreferredSize(new Dimension(925, 70));
     
         next.setIcon(arrow);
         next.setPreferredSize(new Dimension(155, 55));
         next.setVisible(true);
         next.addActionListener(new ActionListener(){
     
             public void actionPerformed(ActionEvent e)
             {
                if ( e.getSource() == next )    
                {
     
                }            
     
             } 
     
           });
     
         lastPanel.add(next);
         mainPanel.add(lastPanel);
     
         JScrollPane pane = new JScrollPane(mainPanel);
     
         getContentPane().add(pane);
     
     
         /*
                        For inside the action buttons:
     
     
                        if (numAns == 1) numAns starts at 5 which is passed into the constructor and gets -- on every action
                        {     
                             next.setVisible(true); the button to click next becomes visible.
                        } 
         */
      }      // END CONSTRUCTOR
     
     
      public void setNumQues(int levels)
      {
         numQues = levels;  
      }
     
      public void setNumItems(int items)
      {
         numItems = items;
      }
     
      public void setPics(String group[])
      {
         //pics[] = new String[numItems];  
     
         for (int counter = 0; counter < numItems; counter++)
         {
             pics[counter] = group[counter];
         }
      }
     
      public void setDescrip(String description[])
      {
         descrip = new String[numItems];  
     
         for (int counter = 0; counter < numItems; counter++)
         {
             descrip[counter] = description[counter];
         }     
      }
     
      public void setLetters(int numLetters[])
      {
         letters = new int[numItems];
     
         for (int counter = 0; counter < numItems; counter++)
         {
             letters[counter] = numLetters[counter];
         }       
      }
     
      public void setImg(String image)
      {
         img = image;
      }
     
      public void setArrow()
      {
         arrow = new ImageIcon("Submit.gif");            // submit arrow
      }
     
      public void setPic()
      {
         pic = new ImageIcon(img);                       // top banner
      }
     
      public void setRandoms()
      {
         randoms = new int[numQues];
     
         while( count < numQues )
         {
            int number = random.nextInt(numItems);          // get 8 random numbers between 1 and 29
     
            boolean containsNumber = false;
     
            for ( int i = 0; i < numQues ; i++ )
            {
               if ( randoms[i] == number )
                 containsNumber = true;   
            }
     
            if ( containsNumber == false && number != 0 )
            {
               randoms[count] = number;
               count++;
            }                
         }
      }
     
      public void setLabelHolder()
      {
         labelHolder = new JLabel[numItems];   
     
         for (int i = 0; i < numItems; i++)
         { 
            labelHolder[i] = new JLabel( pics[i] );  
         }
     
      }
     
      public void setMatchingDesc()
      {
         matchingDesc = new JLabel[numQues];  
     
         for (int i = 0; i < numQues; i++)
         { 
            matchingDesc[i] = new JLabel( descrip[randoms[i]] );  
         }
      }
     
      public void setTextFields()
      {
         textFields = new JTextField[numQues]; 
     
         for (int i = 0; i < numQues; i++)
         { 
            textFields[i] = new JTextField( letters[randoms[i]] );  
         }
      }
     
      public void setIcons()
      {
         icons = new ImageIcon[numQues];
     
         String text;
     
         for (int i = 0; i < numQues; i++)
         {
             text = labelHolder[randoms[i]].getText();  
             icons[i] = new ImageIcon(text); 
         }
      }
     
    } // End class


  5. The Following User Says Thank You to I MIKE C I For This Useful Post:

    TrinnyBoppers (February 28th, 2013)

  6. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: No idea why I'm getting NullPointerException error

    Ahhh... I MIKE C I thank you so much, that is exactly what I needed to alter to fix the problem, and I completely understand what I was doing wrong!!

    Much thanks to you!!

Similar Threads

  1. Using NetBeans and I have no idea what the error message means...
    By Rick Sebastian in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 20th, 2013, 08:30 PM
  2. NullPointerException Error
    By TaoNinja in forum Exceptions
    Replies: 6
    Last Post: December 21st, 2012, 04:43 PM
  3. Error, NullPointerException
    By alex067 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: October 14th, 2012, 09:05 AM
  4. NullPointerException error
    By pyrotecc25 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 14th, 2011, 01:17 PM
  5. NullPointerException error
    By blazerix in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 24th, 2011, 09:59 PM

Tags for this Thread