NullpointerException error.

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

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

#16 natecat  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 225
  • Joined: 19-December 11

Re: NullpointerException error.

Posted 15 January 2013 - 07:40 PM

0*Anything is equal to zero, so on the first iteration of those loops, you will have the width and the height be zero, which isn't allowed.
Was This Post Helpful? 1
  • +
  • -

#17 Gisengryl  Icon User is offline

  • D.I.C Head

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

Re: NullpointerException error.

Posted 15 January 2013 - 08:21 PM

View Postnatecat, on 15 January 2013 - 07:40 PM, said:

0*Anything is equal to zero, so on the first iteration of those loops, you will have the width and the height be zero, which isn't allowed.


These are the 2 for loops that I have in my code.

They look valid to me :o

// adds the 9 buttons with image to puzpiece panel
		             for(int a=0; a<9; a++){
		                 button[a] = new JButton();
		                    puzpiece.add(button[a]);
		             }


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);
		            }
		        }

Was This Post Helpful? 0
  • +
  • -

#18 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: NullpointerException error.

Posted 15 January 2013 - 10:15 PM

Well what natecat wants to say is that in your nested for loops you will have

for(int i=0;i<3;i++){
                    for(int j=0;j<3;j++){


where in the first iteration, i=0 and j=0

Then your line 11 will be

wi = bi.getSubimage(0,0, w/3, h/3);


It would be better if you could change to

for(int i=1;i<3;i++){
                    for(int j=1;j<3;j++){


In that way, the width and height of the image will never equal 0.


regards,
Raghav

This post has been edited by raghav.naganathan: 15 January 2013 - 10:16 PM

Was This Post Helpful? 1
  • +
  • -

#19 Gisengryl  Icon User is offline

  • D.I.C Head

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

Re: NullpointerException error.

Posted 15 January 2013 - 10:55 PM

Thanks raghav.

I still have the same errors though =/.

I did a debug and I believe the error of width = 0 lies in

Image sc = wi.getScaledInstance(puzpiece.getWidth()/3,puzpiece.getHeight()/3, Image.SCALE_AREA_AVERAGING);

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:68)
	at JPRG.GameFrame.initialize(GameFrame.java:40)
	at JPRG.GameFrame.<init>(GameFrame.java:19)
	at JPRG.GameFrame$1.run(GameFrame.java:83)
	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: 16 January 2013 - 12:46 AM

Was This Post Helpful? 0
  • +
  • -

#20 natecat  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 225
  • Joined: 19-December 11

Re: NullpointerException error.

Posted 15 January 2013 - 10:59 PM

Please post the updated code.
Was This Post Helpful? 0
  • +
  • -

#21 Gisengryl  Icon User is offline

  • D.I.C Head

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

Re: NullpointerException error.

Posted 16 January 2013 - 12:46 AM

Updated code is in the following link. I realise pasting code directly results in wrong line numbers.

Link to updated code

On another note, regarding the for loop in which the variable is suggested to be changed to 1 from 0 ..

I thought that it refers to the 3x3 grid, so it should start from 0 as the starting coordinate of subImage() is from (0,0) ?
Was This Post Helpful? 0
  • +
  • -

#22 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: NullpointerException error.

Posted 16 January 2013 - 01:37 AM

Well, you can overcome that by using an if condition

for(int i=0;i<3;i++){
                    for(int j=0;j<3;j++){
                           if(i!=0 || j!=0)
                           { 
                            BufferedImage wi;
                            .
                            . 
                            .
                            setupImage(count++,sc);
                            }
                        }  
                     }


regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#23 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: NullpointerException error.

Posted 16 January 2013 - 04:54 AM

Sometimes while attempting to fix elusive errors or errors we don't understand, we add bandaid after bandaid to a sucking chest wound until we can't see it any more. We then feel like we've done something and wonder why the patient isn't better, but the sucking chest wound remains, and the patient drowns.

Your for loops have lost their way. I think you're trying to do something like this:
        for ( int i=0 ; i <= 2 ; i++ )
        {
            for ( int j = 0 ; j <= 2 ; j++ )
            {
                BufferedImage wi = bi.getSubimage(i*w/3,j*h/3, w/3, h/3);
                Image sc = wi.getScaledInstance( wi.getWidth(),
                           wi.getHeight(), Image.SCALE_AREA_AVERAGING);
                setupImage(count++,sc);
            }
        }

Other things to think about:
- the exception handling is too far from the possible source of the error. There are layers of exception handling that just aren't necessary.
- remove the JFrame.setSize() method, uncomment the pack(), and let ol' Swing nature run its course.
- I personally don't understand the neutered constructor / initialize() method approach to building the user interface. Maybe it's a work in progress and will make more sense later. Even if that were true, I would recommend building the JFrame in the constructor and building other components outside the constructor as necessary and as it makes sense, adding them to the JFrame in the constructor when they're done.
- I still recommend you favor composition over inheritance.
Was This Post Helpful? 1
  • +
  • -

#24 Gisengryl  Icon User is offline

  • D.I.C Head

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

Re: NullpointerException error.

Posted 16 January 2013 - 06:42 AM

@greg :

Haha I love the way you phrase your analogies..

I redid the whole code, taking into account what you said. The only point being that I will use inheritance for the time being for fear of inadvertently breaking the code =/ .

The code runs and the 9 buttons are displayed, but the images are not loaded onto the buttons.

Hmm if I use wi.getScaledInstance as you mentioned, the divided image will scale to itself ? Since variable wi refers to the divided images ? I admit I am not fluent in imageIO at all =/ . Pardon me if I am really talking nonsense over here.

In my code, I planned to divide the original image to 9 parts based on the JPanel(puzpiece) 3x3 GridLayout and then scale it based on the size of the buttons in the JPanel.

The 9 individually divided image pieces are supposed to fit onto each of the 9 Jbuttons created via an array.

package JPRG;

import javax.swing.*;      
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
    
    public class GameFrame2 extends JFrame {
       private JButton[] button = new JButton[9];
       JPanel puzpiece;
       
       public GameFrame2() {
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setTitle("Puzzle Game"); 
            
            // 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));
            // calls setImage() method and catches the IOException
            try {
                setImage();
            } catch (IOException ex) {
               System.out.println("Something's wrong with the image input.");
            }
             // 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.setVisible(true);
            this.setLocationRelativeTo(null);
       }
       
       public static void main(String[] args) {
           GameFrame2 gf = new GameFrame2();
       }
       
        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<=2;i++){
                    for(int j=0;j<=2;j++){
                    BufferedImage wi = bi.getSubimage(i*w/3,j*h/3, w/3, h/3);
                    Image sc = wi.getScaledInstance(wi.getWidth(),wi.getHeight(), Image.SCALE_AREA_AVERAGING);
                    setupImage(count++,sc);
                    }
                }
            }
        
        
        private void setupImage(int a,Image wi) {
            button[a]=new JButton(new ImageIcon(wi));
		    }
    }






This post has been edited by Gisengryl: 16 January 2013 - 06:44 AM

Was This Post Helpful? 0
  • +
  • -

#25 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: NullpointerException error.

Posted 16 January 2013 - 06:50 AM

You're getting closer, but right now Line 32 above is holding you back. You can get rid of it, and I think you'll be almost there.

The reason is you already created the buttons and added the images. Creating a new button wipes that out.
Was This Post Helpful? 1
  • +
  • -

#26 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2559
  • View blog
  • Posts: 10,683
  • Joined: 20-September 08

Re: NullpointerException error.

Posted 16 January 2013 - 06:51 AM

Quote

 BufferedImage wi = bi.getSubimage(i*w/3,j*h/3, w/3, h/3);
Image sc = wi.getScaledInstance(puzpiece.getWidth()/3,puzpiece.getHeight()/3, Image.SCALE_AREA_AVERAGING);

Personally, i would sysout all dimensional parameters to check that they're valid. You might, e.g. find that the dimensions of 'puzpiece' are NOT what you expect

This post has been edited by g00se: 16 January 2013 - 06:52 AM
Reason for edit:: indent

Was This Post Helpful? 1
  • +
  • -

#27 Gisengryl  Icon User is offline

  • D.I.C Head

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

Re: NullpointerException error.

Posted 16 January 2013 - 08:08 AM

View PostGregBrannon, on 16 January 2013 - 06:50 AM, said:

You're getting closer, but right now Line 32 above is holding you back. You can get rid of it, and I think you'll be almost there.

The reason is you already created the buttons and added the images. Creating a new button wipes that out.


View Postg00se, on 16 January 2013 - 06:51 AM, said:

Quote

 BufferedImage wi = bi.getSubimage(i*w/3,j*h/3, w/3, h/3);
Image sc = wi.getScaledInstance(puzpiece.getWidth()/3,puzpiece.getHeight()/3, Image.SCALE_AREA_AVERAGING);

Personally, i would sysout all dimensional parameters to check that they're valid. You might, e.g. find that the dimensions of 'puzpiece' are NOT what you expect

Oh yeah, I created the new JButton objects in setupImage() method already.. Major oversight. Thanks.

The images now displays on the buttons now, but they do not fill up the entire button area.

I read up on overriding the original default JPanel setPreferedSize method as it seems like puzpiece.width() = 0 is because the JPanel hasn't been rendered yet.

Link 1
Link 2

but I can't seem to be able to implement overriding well..

Any push in the right direction is appreciated.

Code:
 class puzpiece2 extends JPanel {
         // not sure what to put in here
 		     final int PWIDTH = 500;
   		     final int PHEIGHT = 200;
           
                  @Override
                public Dimension getPreferredSize() {
              return new Dimension(500, 200);
                }
       }

       
        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 panelWidth = getWidth();  // relevant new code
            int panelHeight = getHeight();  // relevant new code
            int count=0;
                for(int i=0;i<=2;i++){
                    for(int j=0;j<=2;j++){
                    BufferedImage wi = bi.getSubimage(i*w/3,j*h/3, w/3, h/3);
                    Image sc = wi.getScaledInstance(wi.getWidth(),wi.getHeight(), Image.SCALE_AREA_AVERAGING);
                    setupImage(count++,sc);
                    }
                }
            }

This post has been edited by Gisengryl: 16 January 2013 - 08:09 AM

Was This Post Helpful? 0
  • +
  • -

#28 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2559
  • View blog
  • Posts: 10,683
  • Joined: 20-September 08

Re: NullpointerException error.

Posted 16 January 2013 - 08:57 AM

Quote

puzpiece.width() = 0 is because the JPanel hasn't been rendered yet.
That was my point ;)/>/>
The only solid way of dealing with that situation is to arrange things such that the code is executed after it HAS been rendered.One way of doing that would be at the end of an appropriate paintComponent override

You should really load the image in the ctor - move it out of setImage. Then try adding the following

public void paintComponent(Graphics g) {
  super.paintComponent(g);
  setImage();
}

This post has been edited by g00se: 16 January 2013 - 09:01 AM
Reason for edit:: typo

Was This Post Helpful? 0
  • +
  • -

#29 Gisengryl  Icon User is offline

  • D.I.C Head

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

Re: NullpointerException error.

Posted 16 January 2013 - 09:43 AM

Moving the load image to the ctor causes issues for ImageIO to read the image as now both are split into two methods ?

I can't get ImageIO in getImage() method to read img in ctor .

I thought BufferedImage bi=ImageIO.read(GameFrame2.img); would work, but it didn't.

This post has been edited by Gisengryl: 16 January 2013 - 09:43 AM

Was This Post Helpful? 0
  • +
  • -

#30 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2559
  • View blog
  • Posts: 10,683
  • Joined: 20-September 08

Re: NullpointerException error.

Posted 16 January 2013 - 10:29 AM

Quote

Moving the load image to the ctor causes issues for ImageIO to read the image as now both are split into two methods ?
Well, you do of course have to make 'img' an instance variable of your class. Do that and there should be no problem at all
Was This Post Helpful? 0
  • +
  • -

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