Solved

NullPointerException .......

Posted on 2004-09-29
3
185 Views
Last Modified: 2010-03-31
Hi experts,

   I got  a java.lang.NullPointerException at line 00 while I tried to set size for a JButton .... does any one know what i might have missed .... thanks.

--------------------
       :
       :
 getContentPane().setLayout(null);
        getContentPane().setSize(600,600) ;
        getContentPane().setLocation(0,0) ;
        getContentPane().add(myHead) ;
          // set cookies location
        JButton cookies[] = new JButton[10] ;
        for(int i=0; i< cookiesNum; i++){
              int cookies_x, cookies_y ;
             
              cookies_x = ((int)Math.floor(Math.random()*1000)) % 500 ;
              cookies_y = ((int)Math.floor(Math.random()*1000)) % 500 ;
              cookies[i].setSize(20,20) ;                                       // line 00
              cookies[i].setLocation(cookies_x,cookies_y) ;
             
              getContentPane().add(cookies[i]) ;
             
          }
0
Comment
Question by:meow00
  • 2
3 Comments
 
LVL 1

Expert Comment

by:Dave_Toland
Comment Utility
the array is empty.  try somthing like..

JButton cookies[] = new JButton[10] ;
cookies[0] = new JButton("A Button");
cookies[1] = new JButton("B Button");
cookies[2] = new JButton("C Button");
cookies[3] = new JButton("D Button");

        for(int i=0; i< cookiesNum; i++){
              int cookies_x, cookies_y ;
             
              cookies_x = ((int)Math.floor(Math.random()*1000)) % 500 ;
              cookies_y = ((int)Math.floor(Math.random()*1000)) % 500 ;
              cookies[i].setSize(20,20) ;                                       // line 00
              cookies[i].setLocation(cookies_x,cookies_y) ;
             
              getContentPane().add(cookies[i]) ;
             
          }
0
 
LVL 1

Accepted Solution

by:
Dave_Toland earned 250 total points
Comment Utility
actually, if you're array is of size 10 you either need to fill it with objects, set empty cells to null or detect the fact that the cell is empty:

the array is empty.  try somthing like..

JButton cookies[] = new JButton[10] ;
cookies[0] = new JButton("A Button");
cookies[1] = new JButton("B Button");
cookies[2] = new JButton("C Button");
cookies[3] = new JButton("D Button");

   //use a dynamic parameter in the for loop
        for(int i=0; i< cookies.length; i++){
              int cookies_x, cookies_y ;
             
              if(cookies[i] != null){

                      cookies_x = ((int)Math.floor(Math.random()*1000)) % 500 ;
                      cookies_y = ((int)Math.floor(Math.random()*1000)) % 500 ;
                      cookies[i].setSize(20,20) ;                                       // line 00
                      cookies[i].setLocation(cookies_x,cookies_y) ;
             
                      getContentPane().add(cookies[i]) ;
              }
          }
0
 
LVL 3

Assisted Solution

by:CI-Ia0s
CI-Ia0s earned 50 total points
Comment Utility
Except that if you the array to be set up for all 10 values use a for loop. ;)

In fact, you could even put the object creation in your existing loop:

        for(int i=0; i< cookies.length; i++){
              int cookies_x, cookies_y ;
             
              JButton cookies[i] = new JButton("Click me!"); //This is the new line...

              if(cookies[i] != null){

                      cookies_x = ((int)Math.floor(Math.random()*1000)) % 500 ;
                      cookies_y = ((int)Math.floor(Math.random()*1000)) % 500 ;
                      cookies[i].setSize(20,20) ;                                       // line 00
                      cookies[i].setLocation(cookies_x,cookies_y) ;
             
                      getContentPane().add(cookies[i]) ;
              }
          }
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now