Problem opening an Image with Swing
I write an action listener for the choose class but when I try to open an image into my Jpanel I have the following error: (The problem seems to be the Null pointer because the image was not read or.....)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Genim$choose.actionPerformed(Genim.java:219)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
THE CODE IS :
public class choose implements ActionListener {
public void actionPerformed(ActionEvent ae) {
int option = chooser.showOpenDialog(Genim.this);
double [][] F = new double[90000][5];
int i=0;
if (option == JFileChooser.APPROVE_OPTION) {
try{
BufferedReader in = new BufferedReader(new FileReader(chooser.getSelectedFile())); //file contenente i
String getname= chooser.getSelectedFile().getName();
while (true)
{
String s = in.readLine();
if(s==null){
break;
}
StringTokenizer t = new StringTokenizer(s,"\t");
double uno = Double.parseDouble(t.nextToken());
double due = Double.parseDouble(t.nextToken());
double tre = Double.parseDouble(t.nextToken());
double quattro = Double.parseDouble(t.nextToken());
double cinque = Double.parseDouble(t.nextToken());
F[0]=uno;
F[1]=due;
F[2]=tre;
F[3]=quattro;
F[4]=cinque;
if(i<90000) i=i+1;
if(i==89999) accButton.setText("FIRST YEAR ACQUIRED");
}
in.close();
input8=F;
input10=getname.substring(0, 4) ;
}
catch (Exception e)
{
accButton.setText("File input error");
}
}
Image image = null;
try {
// Read from a file
File file = new File(input10+".jpeg");
image = ImageIO.read(file);
} catch (IOException e) {}
image = image.getScaledInstance(200,200,image.SCALE_FAST);
JLabel label = new JLabel(new ImageIcon(image));
buttonPanel.add(label, BorderLayout.EAST); ->ERROR !!!!
contentPane.add(label);-->ERROR!!!!!
}
}
Message was edited by:
princo
[3716 byte] By [
princoa] at [2007-10-3 2:41:28]

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.
The problem is that I want to open an image into the JPanel with the command ImageIO.read().....under the class "choose" but I receive the error as I have show.Thank you
If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.
Image image = null;
try {
// Read from a file
File file = new File(input10+".jpeg");
image = ImageIO.read(file);
} catch (IOException e) {}
image = image.getScaledInstance(200,200,image.SCALE_FAST);
This may or may not be the problem, but this code here is very prone to NullPointerExceptions. You need a "plan B" in your catch block.
Here's a breakdown of what's happening:
1.You declare an image and assign it a null value.
2. Then you try to create a file and read it to get your image.
3. If there was a problem with the file, the catch block (which is empty) is executed.
4. If this is the case, an exception will be thrown from image.getScaledInstance() because image would still have a value of null, since the try block failed.
Example of a catch block you can use while your projects are still going through testing:
catch (XXXException e) {
e.printStackTrace(System.out);
//or System.err.println(e.getMessage());
System.exit(1);
}
EDIT: Forgot to ask, why are you using Image in the first place? Why not just an ImageIcon?
new JLabel(new ImageIcon("myfile.jpg"));
I will try to be clear:
I want to open a file with the class choose and I want also in the same class to load an image with the command ImageIo.read()., to resize it and then to add this image as an icon into the Jpanel.
I don't know why I receive the error in the final part ( LINE 223 and 224 are shown below) of the class choose:
buttonPanel.add(label, BorderLayout.EAST); ->ERROR !!!!
contentPane.add(label);-->ERROR!!!!!
maybe the image is not fully loaded..
I think that davedes has understood my problem but the modify that he proposed doesn't work> The same error.
The class choose is :
public class choose implements ActionListener {
public void actionPerformed(ActionEvent ae) {
int option = chooser.showOpenDialog(Genim.this);
double [][] F = new double[90000][5];
int i=0;
if (option == JFileChooser.APPROVE_OPTION) {
try{
BufferedReader in = new BufferedReader(new FileReader(chooser.getSelectedFile())); //file contenente i
String getname= chooser.getSelectedFile().getName();
while (true)
{
String s = in.readLine();
if(s==null){
break;
}
StringTokenizer t = new StringTokenizer(s,"\t");
double uno = Double.parseDouble(t.nextToken());
double due = Double.parseDouble(t.nextToken());
double tre = Double.parseDouble(t.nextToken());
double quattro = Double.parseDouble(t.nextToken());
double cinque = Double.parseDouble(t.nextToken());
F[i][0]=uno;
F[i][1]=due;
F[i][2]=tre;
F[i][3]=quattro;
F[i][4]=cinque;
if(i<90000) i=i+1;
if(i==89999) accButton.setText("FIRST YEAR ACQUIRED");
}
in.close();
input8=F;
input10=getname.substring(0, 4) ;
}
catch (Exception e)
{
accButton.setText("File input error");
}
}
Image image = null;
try {
// Read from a file
File file = new File("2001.jpeg");
image = ImageIO.read(file);
} catch (IOException e)
{//e.printStackTrace(System.out);
//System.err.println(e.getMessage());
System.exit(1);
}
image = image.getScaledInstance(200,200,image.SCALE_FAST);
JLabel label = new JLabel(new ImageIcon(image));
buttonPanel.add(label, BorderLayout.EAST);
contentPane.add(label);
}
}
The error is:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Genim$choose.actionPerformed(Genim.java:223)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
THANK YOU VERY MUCH FOR YOUR HELP
Message was edited by:
princo
> I will try to be clear:
you have failed.
for future reference, this is an SSCCE
import java.awt.*;
import javax.swing.*;
import java.io.*;
class Choose extends JFrame
{
public Choose()
{
setLocation(300,100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Image image = null;
try
{
File file = new File("test.jpg");
image = javax.imageio.ImageIO.read(file);
}
catch (IOException e){e.printStackTrace();}
image = image.getScaledInstance(200,200,image.SCALE_FAST);
JLabel label = new JLabel(new ImageIcon(image));
getContentPane().add(label);
pack();
}
public static void main(String[] args){new Choose().setVisible(true);}
}
notice there is no filechooser, no actionListener, no loading of a 450,000 element array.
in other words it contains absolutely nothing that is not related to your problem:-
"Problem opening an Image with Swing".
copy/paste the code, change the file name, compile/run and see if you continue
to get your error.
Are you crazy ?As you can see this an action listener class inside the principal class Genim.....It is the class that I use to implement a button......You have failed.null
> Are you crazy ?
I suspect you really don't want help from anybody in the future. Twice you have been asked to produce a SSCCe program. That is code that is compileable and executable so we can see the exact behaviour of your program. Twice you've posted random bits of code that doesn't really help.
You've also been given an SSCCE that shows you how to read an image file.
All you have to do is take the working code and retro fit it into your ActionListener. We are not going to write the code for you.
> As you can see this an action listener class inside the principal class Genim.....perhaps you should read this book. A bit advanced for you, but might
I will do it, thank you. It is a good conseil, but make me the pleasure, read these two books:
http://www.amazon.com/gp/product/0452262429/sr=1-1/qid=1155667668/ref=sr_1_1/104-8736949-6096729?ie=UTF8&s=books
http://www.amazon.com/gp/product/0679882812/sr=8-2/qid=1155667310/ref=pd_bbs_2/104-8736949-6096729?ie=UTF8
It wouldn't be an advisable thing to bite a hand that is trying to feed you... As people have mentioned, the code you have posted was only a small portion of your application, and the line number 223 (or whatever line that was) gave no indication about the actual code line that failed. You have been requested to produce a complete example, that people (on their own free time without being paid) can paste to their environment, check and help you fixing it, but you didn't do so. What do you expect in return - that the people in this forum will continue helping you after you call them crazy? Are we supposed to just guess that the buttonPanel was properly initialized without seeing the code that is supposed to create it?
I think that I have exaggerated saying that michael is crazy. Sometimes we do mistakes.I solved my problem.This is my last message.Thank you.