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();
}
});
}
}
30 Replies - 843 Views - Last Post: 16 January 2013 - 12:29 PM
#1
NullpointerException error.
Posted 15 January 2013 - 03:39 AM
Replies To: NullpointerException error.
#2
Re: NullpointerException error.
Posted 15 January 2013 - 03:55 AM
#3
Re: NullpointerException error.
Posted 15 January 2013 - 03:56 AM
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.
#4
Re: NullpointerException error.
Posted 15 January 2013 - 04:40 AM
- 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
#5
Re: NullpointerException error.
Posted 15 January 2013 - 04:43 AM
GregBrannon, on 15 January 2013 - 03:55 AM, said:
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
#6
Re: NullpointerException error.
Posted 15 January 2013 - 04:57 AM
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)
darek9576, on 15 January 2013 - 03:56 AM, said:
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 ?
#7
Re: NullpointerException error.
Posted 15 January 2013 - 05:07 AM
Quote
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.
#8
Re: NullpointerException error.
Posted 15 January 2013 - 05:20 AM
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)
#9
Re: NullpointerException error.
Posted 15 January 2013 - 06:17 AM
Quote
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."
#10
Re: NullpointerException error.
Posted 15 January 2013 - 07:45 AM
GregBrannon, on 15 January 2013 - 06:17 AM, said:
Quote
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
#11
Re: NullpointerException error.
Posted 15 January 2013 - 08:28 AM
we can't related the line numbers in the trace with the actual code
#12
Re: NullpointerException error.
Posted 15 January 2013 - 09:26 AM
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
#13
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.
#14
Re: NullpointerException error.
Posted 15 January 2013 - 09:52 AM
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.
#15
Re: NullpointerException error.
Posted 15 January 2013 - 06:59 PM
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
|
|

New Topic/Question
Reply



MultiQuote





|