Solved

NullPointerException .......

Posted on 2004-09-29
3
192 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 1

Expert Comment

by:Dave_Toland
ID: 12185912
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
ID: 12185940
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
ID: 12186166
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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

707 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