Null Pointer Exception with image [duplicate] - java

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Loading ImageIcon from JAR or file system
(1 answer)
Closed 6 years ago.
So while making the following program I am getting the errors as mentioned below:
I don't know why the IDE is not able to find my images
and the images are in the same folder as the .java files.
I am using eclipse neon
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at gui_22.gui.<init>(gui.java:24)
at gui_22.MAIN.main(MAIN.java:6)
The code is as follows:
Main Class
import javax.swing.JFrame;
public class MAIN(){
public static void main(String[] args);
gui go = new gui();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(300,200);
go.setVisible(true);
}
}
Gui Class
import java.awt.FlowLayout;
import.java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class gui extends JFrame{
private JButton reg;// to create buttons
private JButton custom;// Same as above
public gui(){
super("The Title"); //Allows access to the superclass.
setLayout(new FlowLayout());
reg = new JButton("reg Button");
add(reg);
Icon b = new ImageIcon(getClass().getResource("b.png"));
Icon x = new ImageIcon(getClass().getResource("a.png"));
custom = new JButton("Custom",b);
custom.setRolloverIcon(x);
add(custom);
HandlerClass handler = new HandlerClass();
reg.addActionListener(handler);//adding a handler for the button
custom.addActionListener(handler);
}
private class HandlerClass implements ActionListener{
public void actionPerformed(ActionEvent event){
JOptionPane.showMessageDialog(null,String.format("%s", event.getActionCommand()));
}
}
}

Related

Jlabel image / ImageIcon [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I'm trying to add some images to those labels,but Eclipse throw me java.lang.NullPointerException and I don't know why?! Please help :}
public class LabelPanel extends JPanel {
public JLabel[] bills;
public LabelPanel() {
setLayout(new FlowLayout());
init();
labelOrder();
}
private void init() {
bills = new JLabel[4];
for (int i = 0; i < bills.length; i++){
bills[0].setIcon(newImageIcon("C:\\users\\Acer\\Documents\\images\\1.jpg"));
bills[1].setIcon(new ImageIcon("C:\\Users\\Acer\\Documents\\images\\2.jpg"));
bills[2].setIcon(new ImageIcon("C:\\Users\\Acer\\Documents\\images\\5.jpg"));
bills[3].setIcon(new ImageIcon("C:\\Users\\Acer\\Documents\\images\\10.jpg"));
bills[4].setIcon(new ImageIcon("C:\\Users\\Acer\\Documents\\images\\20.jpg"));
}
}
private void labelOrder() {
add(bills[0]);
add(bills[1]);
add(bills[2]);
add(bills[3]);
add(bills[4]);
}
}
Try following code
Sample Code
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.FlowLayout;
public class SwingExampleDemo {
public static void main(String[] args) {
JFrame f=new JFrame();//creating instance of JFrame
ImageIcon imageIcon = new ImageIcon("C:\\users\\Acer\\Documents\\images\\1.jpg"); //here image path
JLabel jLabel = new JLabel(imageIcon);
f.add(jLabel);
f.setLayout(new FlowLayout());//using layout managers
f.setVisible(true);//making the frame visible
f.pack();
}
}
Hope It will help you...

java error says JOptionPane cannot be resolved, didn't use JOptionPane

I'm kind of a beginner and I was using a tutorial to make a simple program that displays text fields on a JFrame. I didn't use a JLayeredPane in the whole project, but I still get this error that says, "The type javax.swing.JLayeredPane cannot be resolved. It is indirectly referenced from required .class files." Why do I get this error?
Here's the code (there's two classes):
second class:
package eventHandlerTutorial;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;
public class secondClass extends JFrame
{
private JTextField item1;
private JTextField item2;
private JTextField item3;
private JPasswordField passwordField;
public secondClass()
{
super("The title");
setLayout(new FlowLayout());
item1=new JTextField(10);
add(item1);
item2=new JTextField("enter text here");
add(item2);
item3=new JTextField("uneditable",20);
item3.setEditable(false);
add(item3);
passwordField=new JPasswordField("mypass");
add(passwordField);
theHandler handler=new theHandler();
item1.addActionListener(handler);
item2.addActionListener(handler);
item3.addActionListener(handler);
passwordField.addActionListener(handler);
}
private class theHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String string="";
if(event.getSource()==item1)
string=String.format("field 1: %s",event.getActionCommand());
else if(event.getSource()==item2)
string=String.format("field 2: %s",event.getActionCommand());
else if (event.getSource()==item3)
string=String.format("field 3: %s",event.getActionCommand());
else if(event.getSource()==passwordField)
string=String.format("password field is: %s",event.getActionCommand());
JOptionPane.showMessageDialog(null,string);
}
}
}
main class:
package eventHandlerTutorial;
import javax.swing.JFrame;
public class mainClass
{
public static void main(String[] args)
{
secondClass sc=new secondClass();
sc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sc.setSize(350,100);
sc.setVisible(true);
}
}
The error is explained:
"... cannot be resolved. It is indirectly referenced from required .class files.".
Another thing is, in your question you are saying "JOptionPane"
and
in explanation you are saying "JLayeredPane".
Sometimes (because of IDE errors) you are suppose to import manually.
Add
import javax.swing.JLayeredPane;
manually.
and check it out.
if, still you face issue,
try to clean the project.

Class can`t declare type

I am going through thenewboston's tutorials and I have an unexpected error. I have tried to do everything that Eclipse suggest, but can't figure it out where the problem is.
this is my Main Class
import javax.swing.JFrame;
class Main {
public static void main(String args[]) {
Gui go = new Gui();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(300,200);
go.setVisible(true);
}
}
and this is GUI Class
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class Gui extends JFrame {
private JButton reg;
private JButton custom;
public Gui(){
super("The title");
setLayout(new FlowLayout());
reg = new JButton("Regular Button");
add(reg);
Icon b = new ImageIcon(getClass().getResource("b.png"));
Icon a = new ImageIcon(getClass().getResource("a.png"));
custom = new JButton("Custom", b);
custom.setRolloverIcon(a);
add(custom);
HandlerClass handler = new HandlerClass();
reg.addActionListener(handler);
custom.addActionListener(handler);
}
private class HandlerClass implements ActionListener{
public void actionPerformed(ActionEvent event){
JOptionPane.showMessageDialog(null, String.format("%s", event.getActionCommand()));
}
}
}
Thanks brothers for helping me out!
You've posted a couple of different stacktraces with the error on different line numbers but the code seems to have moved around. The error itself is talking about a NullPointerException in a constructor for ImageIcon. Not really anything to do with the JButton so the tags are misleading.
Basically you're looking up an image location of b.png and a.png. If these two files don't exist then you'll get a runtime exception like you have. The quick fix is to add these two images to the project so they are found.
A more robust solution would be to handle the exception and either output a more meaningful error or just carry on without the icon on the gui.

How to fix blurry Swing elements in Eclipse [duplicate]

This question already has answers here:
Swing rendering appears broken in JDK 1.8, correct in JDK 1.7
(8 answers)
Closed 8 years ago.
I created a GUI with one JButton in it but it just shows a blurry button.
Here you see what I mean.
This is the code of my main class:
Gui gui = new Gui();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(300, 300);
gui.setVisible(true);
Code of gui class:
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.awt.FlowLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Gui extends JFrame {
public Gui() {
super("testing buttons");
getContentPane().setLayout(new FlowLayout());
JButton btnClickMe = new JButton("Click me");
getContentPane().add(btnClickMe);
}
}
Does anyone know how to fix this?
There is no reason to call
super("testing buttons");
To set the title, just use:
gui.setTitle("testing buttons");
What operating system are you running?

Java ImageIcon/Icon and JLabel is not working

Why is it that my code is not showing the image that I inserted? there's no compilation error or Syntax error but why is it like that?
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.SwingConstants;
public class FirstUI extends JFrame{
private JLabel firstlabel;
private JLabel secondLabel;
private JLabel pie;
public FirstUI(){
super("Tittle");
setLayout(new FlowLayout());
firstlabel = new JLabel("Hello World");
firstlabel.setToolTipText("Hello World");
String path = "pie.png";
Icon pie = new ImageIcon(path);
secondLabel = new JLabel("Text with icon",pie,SwingConstants.LEFT);
add(secondLabel);
add(firstlabel);
}
}
main class
import javax.swing.JFrame;
public class FirstUiTest {
public static void main(String[] args){
FirstUI MyUI = new FirstUI();
MyUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyUI.setSize(320,280);
MyUI.setVisible(true);
}
}
if the "pie.png" is in the same Path of FirstUI.class try to use:
Icon pie = new ImageIcon(ImageIO.read( FirstUI.class.getResourceAsStream( "pie.png" ) ) );
I tried this exact code, and it worked. It looks like pie.png cannot be found. If you're using eclipse, put it in the project root (The same folder that has /bin and /src). Otherwise, put it in the same directory where you run the java command from.

Categories