NullpointerException error.

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

30 Replies - 1718 Views - Last Post: 16 January 2013 - 12:29 PM Rate Topic: -----

#1 Gisengryl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 129
  • Joined: 08-September 12

NullpointerException error.

Posted 15 January 2013 - 03:39 AM

Any help is appreciated thanks a lot . At my wits end =/

			public class GameFrame extends JFrame {

			private JButton[] button = new JButton[9];
                        private JPanel puzpiece;
                        private JFrame frame;
                        
                        public GameFrame() {
                            try {
                                initialize();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }

			public void initialize() throws IOException {
                            frame = new JFrame();

                        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		        
		         frame.setTitle("Puzzle Game");       
		         frame.setVisible(true);
                // run setImage()
                         setImage();
		        // creates a new panel for the splitted puzzle pieces
		        puzpiece = new JPanel();
		        // set layout of puzpiece panel
		        puzpiece.setLayout(new GridLayout(3,3));
		        // set size of puzpiece panel
		         puzpiece.setPreferredSize(new Dimension(500,200));

		        // adds 9 buttons to puzpiece panel
		             for(int a=0; a<9; a++){
		                 button[a] = new JButton();
		                    puzpiece.add(button[a]);
		             }
		        // add puzpiece panel to JFrame
		        frame.add(puzpiece,BorderLayout.WEST);
                        
                        // set frame size to follow the maximum sizes of all contained components 
                        frame.pack();
                        frame.setLocationRelativeTo(null);
		    }

			    public void setImage() throws IOException{
		        URL img= GameFrame.class.getResource("image/Penguins3.jpg");
		        BufferedImage bi=ImageIO.read(img);
		        int w=bi.getWidth();
		        int h=bi.getHeight();
		        int count=0;
		        for(int i=0;i<3;i++){
		            for(int j=0;j<3;j++){
		                BufferedImage wi=bi.getSubimage(i*w/3,j*h/3, w/3, h/3);
		                Image sc=bi.getScaledInstance(puzpiece.getWidth()/3,


		               puzpiece.getHeight()/3, Image.SCALE_AREA_AVERAGING);
		                setupImage(count++,sc);
		            }
		        }
		    }


		       private void setupImage(int a,Image wi) {
		        button[a]=new JButton(new ImageIcon(wi));
		    }
                       
                       public static void main(String[] args){
                            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    GameFrame gf = new GameFrame();
                }
                });
    }
                       }
				



Is This A Good Question/Topic? 0
  • +

Replies To: NullpointerException error.

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2204
  • View blog
  • Posts: 5,236
  • Joined: 10-September 10

Re: NullpointerException error.

Posted 15 January 2013 - 03:55 AM

You've been around long enough to know you should post the error, copied and pasted, correlated to the posted code as needed.
Was This Post Helpful? 2
  • +
  • -

#3 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,693
  • Joined: 13-March 10

Re: NullpointerException error.

Posted 15 January 2013 - 03:56 AM

Why dont you narrow down places where the error can occur. For example, in your initialize() method, comment out the setImage() method and if everything runs OK then you know the error is in your setImage() method, otherwise, setImage() is good.

In your setImage() method, you call setupImage() that does something to the array of Buttons but then you override the values in the initialize() method. This will not cause NPE but the logic of it is wrong.
Was This Post Helpful? 1
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,885
  • Joined: 06-March 08

Re: NullpointerException error.

Posted 15 January 2013 - 04:40 AM

Firts I don't see how your initialize() method can throw an IOException so:
- no reason to declare that it can do so
- no reason to call it within a try/catch clause statements
Coding like that will require more try/catch statements than regular Java statement

Second GameFrame extends JFrame so it is a JFrame no need to create another on withon its constructor

             frame = new JFrame();  
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                  
             frame.setTitle("Puzzle Game");         


should be
             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                  
             setTitle("Puzzle Game");         



Fixc that, post your errors stack and we'll see
Was This Post Helpful? 0
  • +
  • -

#5 Gisengryl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 129
  • Joined: 08-September 12

Re: NullpointerException error.

Posted 15 January 2013 - 04:43 AM

View PostGregBrannon, on 15 January 2013 - 03:55 AM, said:

You've been around long enough to know you should post the error, copied and pasted, correlated to the posted code as needed.


Really sorry, I guess I didn't take note.

My first time posting regarding java errors.

I will make amends soon enough, after reading all replies :) .
Was This Post Helpful? 0
  • +
  • -

#6 Gisengryl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 129
  • Joined: 08-September 12

Re: NullpointerException error.

Posted 15 January 2013 - 04:57 AM

Error code after reading through pbl's comments. setImage() is causing errors even before execution- an unreported IOException error.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at JPRG.GameFrame.initialize(GameFrame.java:22)
	at JPRG.GameFrame.<init>(GameFrame.java:17)
	at JPRG.GameFrame$1.run(GameFrame.java:74)
	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
	at java.awt.EventQueue.access$200(EventQueue.java:103)
	at java.awt.EventQueue$3.run(EventQueue.java:682)
	at java.awt.EventQueue$3.run(EventQueue.java:680)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)


View Postdarek9576, on 15 January 2013 - 03:56 AM, said:

Why dont you narrow down places where the error can occur. For example, in your initialize() method, comment out the setImage() method and if everything runs OK then you know the error is in your setImage() method, otherwise, setImage() is good.

In your setImage() method, you call setupImage() that does something to the array of Buttons but then you override the values in the initialize() method. This will not cause NPE but the logic of it is wrong.

Hmm now that you mention, I did overwrite it.. That's is because I did a loop to create the buttons.

Does using a different variable name for the for loop used in creation of the 9 buttons in intialize() method help ?
Was This Post Helpful? 0
  • +
  • -

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2204
  • View blog
  • Posts: 5,236
  • Joined: 10-September 10

Re: NullpointerException error.

Posted 15 January 2013 - 05:07 AM

pbl said:

Quote

Second GameFrame extends JFrame so it is a JFrame no need to create another on withon its constructor

The preferred approach is to not extend JFrame if you don't need to: Remove the extension and stick with creating the JFrame instance unless you absolutely need the extension.

I believe your original NPE was due to calling setImage() before creating an instance of JPanel and storing it in puzpiece in the initialize() method.
Was This Post Helpful? 2
  • +
  • -

#8 Gisengryl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 129
  • Joined: 08-September 12

Re: NullpointerException error.

Posted 15 January 2013 - 05:20 AM

@Greg

Oh I see, noted. Is there any reasons behind it ?

This is the original error.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at JPRG.GameFrame.setImage(GameFrame.java:62)
	at JPRG.GameFrame.initialize(GameFrame.java:32)
	at JPRG.GameFrame.<init>(GameFrame.java:18)
	at JPRG.GameFrame$1.run(GameFrame.java:79)
	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
	at java.awt.EventQueue.access$200(EventQueue.java:103)
	at java.awt.EventQueue$3.run(EventQueue.java:682)
	at java.awt.EventQueue$3.run(EventQueue.java:680)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

Was This Post Helpful? 0
  • +
  • -

#9 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2204
  • View blog
  • Posts: 5,236
  • Joined: 10-September 10

Re: NullpointerException error.

Posted 15 January 2013 - 06:17 AM

Quote

Oh I see, noted. Is there any reasons behind it ?

The quick, easy, and lazy (?) answer is that since the Java programmer only can extend once, he/she shouldn't use the silver bullet unless absolutely necessary.

A better, more detailed answer with the underlying reasons can be found in discussions of favoring composition over inheritance, and you can find several discussions of varying quality and depth on the web by Googling "composition over inheritance."
Was This Post Helpful? 2
  • +
  • -

#10 Gisengryl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 129
  • Joined: 08-September 12

Re: NullpointerException error.

Posted 15 January 2013 - 07:45 AM

View PostGregBrannon, on 15 January 2013 - 06:17 AM, said:

Quote

Oh I see, noted. Is there any reasons behind it ?

The quick, easy, and lazy (?) answer is that since the Java programmer only can extend once, he/she shouldn't use the silver bullet unless absolutely necessary.

A better, more detailed answer with the underlying reasons can be found in discussions of favoring composition over inheritance, and you can find several discussions of varying quality and depth on the web by Googling "composition over inheritance."


Okay thanks alot :)
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,885
  • Joined: 06-March 08

Re: NullpointerException error.

Posted 15 January 2013 - 08:28 AM

As you haven't post all your cklass (including the import statements)
we can't related the line numbers in the trace with the actual code
Was This Post Helpful? 0
  • +
  • -

#12 Gisengryl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 129
  • Joined: 08-September 12

Re: NullpointerException error.

Posted 15 January 2013 - 09:26 AM

Okay, after some thought, I solved the NPE issue. I re-added back the exceptions though. Still not clear about the exceptions and why do we have to catch it twice in main method and GameFrame constructor. But somehow it solves the thread "AWT-EventQueue-0" error after adding them. Without the exceptions, the error result is the same, except for the "AWT-EventQueue-0" prepended infront of the 1st line of the error code.

But now there's the java.lang.IllegalArgumentException: Width (0) and height (0) must be non-zero error.

Does this error mean that there is some error in importing the image ?

Thanks.

package JPRG;
import javax.swing.*;      
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

public class GameFrame extends JFrame {

			private JButton[] button = new JButton[9];
                         JPanel puzpiece;
                        
                        public GameFrame() {
                            try {
                                initialize();
                        }catch (IOException e) {
            e.printStackTrace();
        }
                        }

			public void initialize() throws IOException {

                        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		        
		         this.setTitle("Puzzle Game");       
		         
                         
                         
		        // creates a new panel for the splitted puzzle pieces
		        puzpiece = new JPanel();
                        // calls setImage() method
                        setImage();
		        // set layout of puzpiece panel
		        puzpiece.setLayout(new GridLayout(3,3));
		        // set size of puzpiece panel
		         puzpiece.setPreferredSize(new Dimension(500,200));
                         
		        // adds the 9 buttons with image to puzpiece panel
		             for(int a=0; a<9; a++){
		                 button[a] = new JButton();
		                    puzpiece.add(button[a]);
		             }
                             
                             
		        // add puzpiece panel to JFrame
		        this.add(puzpiece,BorderLayout.WEST);
                        
                        // set this size to follow the maximum sizes of all contained components 
//                        this.pack();
                        this.setSize(1500,1200);
                        this.setVisible(true);
                        this.setLocationRelativeTo(null);
		    }

			    public void setImage() throws IOException{
		        URL img= GameFrame.class.getResource("image/Penguins3.jpg");
		        BufferedImage bi=ImageIO.read(img);
		        int w=bi.getWidth();
		        int h=bi.getHeight();
		        int count=0;
		        for(int i=0;i<3;i++){
		            for(int j=0;j<3;j++){
		                BufferedImage wi;
                                wi = bi.getSubimage(i*w/3,j*h/3, w/3, h/3);
		                Image sc;
                                sc = wi.getScaledInstance(puzpiece.getWidth()/3,puzpiece.getHeight()/3, Image.SCALE_AREA_AVERAGING);
		                setupImage(count++,sc);
		            }
		        }
                            }


		       private void setupImage(int a,Image wi) {
		        button[a]=new JButton(new ImageIcon(wi));
		    }
                       
                           public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                	GameFrame gf = new GameFrame();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
                       
 
                       }
			



Error code:
java.lang.IllegalArgumentException: Width (0) and height (0) must be non-zero
	at java.awt.image.ReplicateScaleFilter.<init>(ReplicateScaleFilter.java:102)
	at java.awt.image.AreaAveragingScaleFilter.<init>(AreaAveragingScaleFilter.java:77)
	at java.awt.Image.getScaledInstance(Image.java:171)
	at JPRG.GameFrame.setImage(GameFrame.java:70)
	at JPRG.GameFrame.initialize(GameFrame.java:36)
	at JPRG.GameFrame.<init>(GameFrame.java:19)
	at JPRG.GameFrame$1.run(GameFrame.java:85)
	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
	at java.awt.EventQueue.access$200(EventQueue.java:103)
	at java.awt.EventQueue$3.run(EventQueue.java:682)
	at java.awt.EventQueue$3.run(EventQueue.java:680)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

This post has been edited by Gisengryl: 15 January 2013 - 06:56 PM

Was This Post Helpful? 0
  • +
  • -

#13 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2794
  • View blog
  • Posts: 11,830
  • Joined: 20-September 08

Re: NullpointerException error.

Posted 15 January 2013 - 09:33 AM

Quote

at JPRG.GameFrame.setImage(GameFrame.java:68)

I'm not convinced that line number corresponds to the one in your source code, as opposed to the line number after posting here. Does it? If not, please indicate the real line in the source posted above.
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,885
  • Joined: 06-March 08

Re: NullpointerException error.

Posted 15 January 2013 - 09:52 AM

Your setVisible(true) to the JFrame should be done after all components are inside it
As you did a pack() and there was nothing to display the size of the JFrame will be 0,0 and the JPanel inside too

1) do your setVisible(true) when every thing is added
2) do not use pack() on a JFrame thta contains component that I draw by hand. Pack evaluates the size of the JFrame based on the JComponent inside it. Use the setSize() method.
Was This Post Helpful? 0
  • +
  • -

#15 Gisengryl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 129
  • Joined: 08-September 12

Re: NullpointerException error.

Posted 15 January 2013 - 06:59 PM

@ g00se : Code and error code are both edited after correction based on pbl's comments.

Edit: I realised the line numbers are always wrong after pasting.

Here's the code @ pastebay. Link

@ pbl : code is edited :)/>

This post has been edited by Gisengryl: 15 January 2013 - 07:01 PM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3