create a java program using eclipse, cant run it using netbeans [duplicate] - java

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Errors running Java program
I am currently writing a java program using eclipse. I heard that it is easier to make java gui using netbeans so I want to try to finish this java program using netbeans. However, when i open my project using netbeans and run it, it give me an error. Im tying to use betbeans run the same program that run fine under eclipse.
this is the error:
run:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:181)
at MainForm.addComponentsToPane(MainForm.java:28)
at MainForm.createAndShowGUI(MainForm.java:112)
at MainForm.access$000(MainForm.java:15)
at MainForm$4.run(MainForm.java:125)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:641)
at java.awt.EventQueue.access$000(EventQueue.java:84)
at java.awt.EventQueue$1.run(EventQueue.java:602)
at java.awt.EventQueue$1.run(EventQueue.java:600)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:611)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
BUILD SUCCESSFUL (total time: 8 seconds)
this is the codes im tying to run:
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MainForm {
public static void addComponentsToPane(final JFrame frame, Container pane) {
Color colorGreen = new Color(0, 100, 0);
Color colorBrown = new Color(150, 100, 0);
//Color colorBlue = new Color (0, 0, 150);
//Font font = new Font("Verdana", Font.BOLD, 12);
pane.setLayout(null);
pane.setBackground(new Color (255, 255, 170));
//add image and necessary labels on top left corner
//SHA Image and label
ImageIcon image = new ImageIcon(MainForm.class.getResource("SHA_logo.gif"));
JLabel label = new JLabel("Office of Traffic & Safety", image, JLabel.LEFT);
label.setVerticalTextPosition(JLabel.BOTTOM);
label.setHorizontalTextPosition(JLabel.CENTER);
label.setFont(new Font("Times New Roman", Font.PLAIN, 11));
pane.add(label);
label.setBounds(50, 10, 130, 100);
label.setBorder(null);
//university of maryland image and label\\\
image = new ImageIcon(MainForm.class.getResource("maryland_flag_round.gif"));
label = new JLabel("Univ. of Maryland", image, JLabel.LEFT);
label.setVerticalTextPosition(JLabel.BOTTOM);
label.setHorizontalTextPosition(JLabel.CENTER);
label.setFont(new Font("Times New Roman", Font.PLAIN, 11));
pane.add(label);
label.setBounds(190, 10, 130, 90);
label.setBorder(null);
//critical lane label
label = new JLabel("Critical Lane Volume");
label.setFont(new Font("Arial Narrow", Font.BOLD, 30));
label.setForeground(colorGreen);
pane.add(label);
label.setBounds(50, 90, 250, 50);
label.setBorder(null);
label = new JLabel("<html>Please choose the analysis type:</html>");
label.setFont(new Font("Arial", Font.BOLD, 18));
label.setForeground(colorBrown);
pane.add(label);
label.setBounds(25, 130, 300, 70);
label.setBorder(null);
//back and exit buttons
JButton button1 = new JButton("DIAT");
JButton button2 = new JButton ("Intersection");
JButton button3 = new JButton ("Exit");
pane.add(button1);
pane.add(button2);
pane.add(button3);
button1.setBounds(75, 200, 200, 50);
button2.setBounds(75, 270, 200, 50);
button3.setBounds(75, 340, 200, 50);
//add attap label at bottom left
image = new ImageIcon(MainForm.class.getResource("attap_logo.gif"));
label = new JLabel(image);
pane.add(label);
label.setBounds(30, 380, 270, 90);
label.setBorder(null);
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
frame.setVisible(false);
frame.dispose();
InterchangeLoad.main(null);
}
});
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
frame.setVisible(false);
frame.dispose();
MultipleIntersectionLoad.main(null);
}
});
button3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
frame.setVisible(false);
frame.dispose();
}
});
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Critical Lane Volume");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
addComponentsToPane(frame, frame.getContentPane());
frame.setSize(350, 500);
frame.setResizable(false);
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

It's not finding your images. Are you sure that you have your images in the same relative position? Are they in the same location as the MainForm.class file? Also I would take exception to the comment that it is easier to create a GUI with NetBeans than Eclipse. I've used both and find both fine to use, but I recommend you avoid using any code generation software until you know Swing well. For instance, you would do well to learn how to use the Swing/AWT layout managers and avoiding using null layout/setBounds.

Your program can't find the file "SHA_logo.gif", so your new ImageIcon(...) is new ImageIcon(null) and that is causing the NullPointerException.

Related

I am trying to put an imageicon in my gui but it doesn't appear when I run the code. Can somebody tell me how to fix it

This is the code that I am using right now. When i run the code the image doesn't show.
import javax.swing.*;
import java.awt.*;
public class Interface {
private static JLabel name;
private static ImageIcon image = new ImageIcon("DigiD.png");
public static void main(String[] args) {
//Panel
JPanel panel = new JPanel();
panel.setBackground(new Color(255, 145, 77, 255));
//Titel van de app
name = new JLabel("Food 4 U");
name.setFont(new Font("Gill Sans Ultra Bold", Font.BOLD, 20));
name.setBounds(135, 1, 1000, 50);
JLabel logo = new JLabel(image);
panel.add(logo);
//Frame
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setSize(400, 1000);
frame.getContentPane().setBackground(new Color(255, 145, 77, 255));
frame.add(panel);
frame.setVisible(true);
}
}
Works fine for me. Make sure the image is inside the correct folder of your project. (directly inside the top folder with the name of the entire project)

How do I properly display radio buttons on the screen along with other elements? (images attached) - Java GUI using NetBeans

I am trying to create a pizza ordering system where the user is presented with two choices for pizza sauces (tomato and barbeque). I have already created the GUI and have already coded the design for the radio buttons, but I am not sure how to display the radio buttons on screen along with other elements. I have tried placing it within the paint code and the main aswell, but it changes the colour of the GUI.
I have attached two images. First one is the GUI which is meant to have the radio buttons placed underneath the text (pizza, base&sauce, topping).
Second one is the GUI I get when I add in radio button code.
//Code
package pizzaorder2;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
/**
*
* #author sm
*/
public class PizzaOrder2 extends JFrame {
static JFrame frame;
static String[] pizza = {"Supremo Supreme", "Supreme", "Chicken",
"Aussie",
"Vegie", "Hawaiian"};
static String[] base = {"Thin and Crispy", "Pan", "Cheese filled
crust"};
static String[] topping = {"None", "Pepperoni", "Salami", "Ham",
"Bacon", "Chicken", "Onion"};
static String tomatoString = "Tomato";
static String barbequeString = "Barbeque";
#Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.WHITE);
g.setFont(new Font("Serif", Font.BOLD + Font.ITALIC, 55));
g.drawString("Pizza Order", 175, 70);
Image imagepizza = new ImageIcon("/Users/supriyamayuri/NetBeansProjects/DrawGraphics/src/piz.jpg").getImage();
g.drawImage(imagepizza, 20, 90, 140, 120, this);
Image imagesauce = new ImageIcon("/Users/supriyamayuri/NetBeansProjects/DrawGraphics/src/sauce.jpg").getImage();
g.drawImage(imagesauce, 220, 90, 150, 120, this);
Image imagetopping = new ImageIcon("/Users/supriyamayuri/NetBeansProjects/DrawGraphics/src/top.jpg").getImage();
g.drawImage(imagetopping, 430, 90, 140, 120, this);
g.setColor(Color.WHITE);
g.setFont(new Font("Helvetica", Font.BOLD, 25));
g.drawString("Pizza", 40, 240);
g.setColor(Color.WHITE);
g.setFont(new Font("Helvetica", Font.BOLD, 25));
g.drawString("Base & Sauce", 210, 240);
g.setColor(Color.WHITE);
g.setFont(new Font("Helvetica", Font.BOLD, 25));
g.drawString("Topping", 450, 240);
}
public PizzaOrder2() {
//Create the radio buttons.
JRadioButton tomatoButton = new JRadioButton(tomatoString);
tomatoButton.setMnemonic(KeyEvent.VK_B);
tomatoButton.setActionCommand(tomatoString);
JRadioButton barbequeButton = new JRadioButton(barbequeString);
barbequeButton.setMnemonic(KeyEvent.VK_C);
barbequeButton.setActionCommand(barbequeString);
//Group the radio buttons.
ButtonGroup group = new ButtonGroup();
group.add(tomatoButton);
group.add(barbequeButton);
//Put the radio buttons in a column in a panel.
JPanel radioPanel = new JPanel();
radioPanel.add(tomatoButton);
radioPanel.add(barbequeButton);
add(radioPanel);
}
public static void main(String[] args) {
JFrame frame = new PizzaOrder2();
frame.setSize(600, 600);
frame.getContentPane().setBackground(new Color(40, 80, 120));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

Making a second frame in Java swing

I have made a simple digital business card in Java swing using Eclipse, I am brand new to Java and programming in general so please bear with me. Basically what I want to do is add a button that says "Our Work" or "Portfolio" for example, which when clicked will open a new window where I can then add pictures or links or false reviews etc.
I know this is probably quite simple, but I struggle to understand a lot of tutorials when it is based on other peoples code.
package dbuscard;
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.Font;
import javax.swing.JSeparator;
public class card {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
card window = new card();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public card() {
initialize();
}
/**
* Initialise the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(new Color(153, 204, 255));
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblLogo = new JLabel("");
lblLogo.setBounds(127, 11, 219, 104);
frame.getContentPane().add(lblLogo);
lblLogo.setIcon(new ImageIcon("Images\\zlogoimg.png"));
JLabel lblNewLabel = new JLabel("t: 01254 777494");
lblNewLabel.setFont(new Font("Source Code Pro Light", Font.PLAIN, 11));
lblNewLabel.setBounds(20, 187, 126, 14);
frame.getContentPane().add(lblNewLabel);
JLabel lblEApexuxdesigngmailcom = new JLabel("e: apexuxdesign#gmail.com");
lblEApexuxdesigngmailcom.setFont(new Font("Source Code Pro Light", Font.PLAIN, 11));
lblEApexuxdesigngmailcom.setBounds(20, 204, 200, 14);
frame.getContentPane().add(lblEApexuxdesigngmailcom);
JLabel lblVisitWebsite = new JLabel("visit website");
lblVisitWebsite.setFont(new Font("Source Code Pro Light", Font.BOLD, 11));
lblVisitWebsite.setBounds(10, 237, 117, 14);
frame.getContentPane().add(lblVisitWebsite);
JLabel facebook = new JLabel("");
facebook.setBounds(282, 204, 64, 47);
frame.getContentPane().add(facebook);
facebook.setIcon(new ImageIcon("Images\\facebook.png"));
JLabel twitter = new JLabel("");
twitter.setBounds(320, 204, 72, 47);
frame.getContentPane().add(twitter);
twitter.setIcon(new ImageIcon("Images\\twitter.png"));
JLabel youtube = new JLabel("");
youtube.setBounds(356, 204, 68, 47);
frame.getContentPane().add(youtube);
youtube.setIcon(new ImageIcon("Images\\youtube.png"));
JLabel lblSeanHutchinson = new JLabel("Sean Hutchinson");
lblSeanHutchinson.setFont(new Font("Source Code Pro Light", Font.PLAIN, 11));
lblSeanHutchinson.setBounds(20, 128, 126, 14);
frame.getContentPane().add(lblSeanHutchinson);
JLabel lblUxDesigner = new JLabel("UX Designer");
lblUxDesigner.setFont(new Font("Source Code Pro Light", Font.PLAIN, 11));
lblUxDesigner.setBounds(20, 145, 107, 14);
frame.getContentPane().add(lblUxDesigner);
JLabel lblNewLabel_1 = new JLabel("CEO - Apex UX Design");
lblNewLabel_1.setFont(new Font("Source Code Pro Light", Font.PLAIN, 11));
lblNewLabel_1.setBounds(20, 162, 158, 14);
frame.getContentPane().add(lblNewLabel_1);
}
}
Simple. Add an action listener which uses the method setVisible(true); on a seperate JFrame. Example code:
package com.nonsense;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class del {
private JFrame frame, frame2;
/**
* Launch the application.
*/
public static void main(String[] args) {
del window = new del();
window.frame.setVisible(true);
}
/**
* Create the application.
*/
public del() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame2 = new JFrame();
frame2.setBounds(100, 100, 450, 300);
frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame2.getContentPane().setLayout(null);
JButton btnOpenWindow = new JButton("Open Window");
btnOpenWindow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame2.setVisible(true);
}
});
btnOpenWindow.setBounds(167, 118, 120, 23);
frame.getContentPane().add(btnOpenWindow);
}
}
I use "DISPOSE_ON_CLOSE" so that the second window does not terminate the program when the X button is pressed.

How do I remove a component from a JPanel? and then redisplay the frame?

Some context for this code,
what I am trying to do is create a validation warning if the input containts "" as a value,
so if an error is displayed it displays the message.
If it is valid it does not display a message,
so how do I remove the message when the textField is valid?
The message is a JPanel that contains a JLabel with the text in it,
I add this this JPanel to the frame when it is not valid,
and I am trying to remove it when it is valid.
So what am I doing wrong here?
I am at a basic level with Swing.
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Test {
private JFrame frame;
private JTextField textField;
private JTextField textField_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test window = new Test();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Test() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 401, 232);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(10, 11, 330, 94);
frame.getContentPane().add(panel);
panel.setLayout(null);
JLabel lblNewLabel = new JLabel("Firstname :");
lblNewLabel.setBounds(10, 11, 104, 14);
panel.add(lblNewLabel);
textField = new JTextField();
textField.setBounds(76, 8, 244, 20);
panel.add(textField);
textField.setColumns(10);
JLabel lblLastname = new JLabel("Lastname :");
lblLastname.setBounds(10, 42, 78, 14);
panel.add(lblLastname);
textField_1 = new JTextField();
textField_1.setBounds(76, 39, 244, 20);
panel.add(textField_1);
textField_1.setColumns(10);
JButton btnValidate = new JButton("Validate");
btnValidate.addMouseListener(new MouseAdapter() {
#SuppressWarnings("deprecation")
#Override
public void mousePressed(MouseEvent arg0) {
JPanel panel_1 = new JPanel();
JPanel panel_2 = new JPanel();
if(textField.getText().equals("")) {
panel_1.setBackground(new Color(30, 144, 255));
panel_1.setBounds(100, 116, 330, 26);
JLabel lblMessage = new JLabel("0 :");
lblMessage.setForeground(new Color(255, 255, 255));
lblMessage.setFont(new Font("Tahoma", Font.BOLD, 13));
panel_1.add(lblMessage);
frame.getContentPane().add(panel_1);
frame.revalidate();
frame.repaint(10);
frame.revalidate();
}
else if(textField_1.getText().equals("")) {
panel_2.setBackground(new Color(50, 200, 255));
panel_2.setBounds(10, 134, 330, 26);
JLabel lblMessage = new JLabel("1 :");
lblMessage.setBounds(50, 50, 50, 50);
lblMessage.setAlignmentX(50);
lblMessage.setForeground(new Color(255, 255, 255));
lblMessage.setFont(new Font("Tahoma", Font.BOLD, 13));
panel_2.add(lblMessage);
frame.getContentPane().add(panel_2);
frame.remove(panel_1);
frame.revalidate();
frame.repaint(10);
frame.revalidate();
}
}
});
btnValidate.setBounds(231, 71, 89, 23);
panel.add(btnValidate);
}
}
The easiest way is to simply adjust the visibility (JComponent#setVisible( false ) ).
If you really want to remove the component completely, you have to remove and revalidate, as documented in the Container#remove method
This method changes layout-related information, and therefore, invalidates the component hierarchy. If the container has already been displayed, the hierarchy must be validated thereafter in order to reflect the changes.
which results in code like
panel.remove( componentToRemove );
panel.revalidate();
panel.repaint();
As a side note: please replace the null layout and those setBounds call by a proper LayoutManager. You might want to take a look excellent 'Nested layout example' available on SO to see what is possible with layout managers. The Swing tag info on SO contains some extra useful links when starting to work with layout managers
yourpanel.setVisible(false); should hide your panel, where "yourPanel" is your JPanel instance

Errors running Java program

Im tying to run my java program using netbeans and im getting this error, any suggestion?
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:181)
at MainForm.addComponentsToPane(MainForm.java:28)
at MainForm.createAndShowGUI(MainForm.java:112)
at MainForm.access$000(MainForm.java:15)
at MainForm$4.run(MainForm.java:125)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:641)
at java.awt.EventQueue.access$000(EventQueue.java:84)
at java.awt.EventQueue$1.run(EventQueue.java:602)
at java.awt.EventQueue$1.run(EventQueue.java:600)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:611)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
this is the code:
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MainForm {
public static void addComponentsToPane(final JFrame frame, Container pane) {
Color colorGreen = new Color(0, 100, 0);
Color colorBrown = new Color(150, 100, 0);
//Color colorBlue = new Color (0, 0, 150);
//Font font = new Font("Verdana", Font.BOLD, 12);
pane.setLayout(null);
pane.setBackground(new Color (255, 255, 170));
//add image and necessary labels on top left corner
//SHA Image and label
ImageIcon image = new ImageIcon(MainForm.class.getResource("SHA_logo.gif"));
JLabel label = new JLabel("Office of Traffic & Safety", image, JLabel.LEFT);
label.setVerticalTextPosition(JLabel.BOTTOM);
label.setHorizontalTextPosition(JLabel.CENTER);
label.setFont(new Font("Times New Roman", Font.PLAIN, 11));
pane.add(label);
label.setBounds(50, 10, 130, 100);
label.setBorder(null);
//university of maryland image and label\\\
image = new ImageIcon(MainForm.class.getResource("maryland_flag_round.gif"));
label = new JLabel("Univ. of Maryland", image, JLabel.LEFT);
label.setVerticalTextPosition(JLabel.BOTTOM);
label.setHorizontalTextPosition(JLabel.CENTER);
label.setFont(new Font("Times New Roman", Font.PLAIN, 11));
pane.add(label);
label.setBounds(190, 10, 130, 90);
label.setBorder(null);
//critical lane label
label = new JLabel("Critical Lane Volume");
label.setFont(new Font("Arial Narrow", Font.BOLD, 30));
label.setForeground(colorGreen);
pane.add(label);
label.setBounds(50, 90, 250, 50);
label.setBorder(null);
label = new JLabel("<html>Please choose the analysis type:</html>");
label.setFont(new Font("Arial", Font.BOLD, 18));
label.setForeground(colorBrown);
pane.add(label);
label.setBounds(25, 130, 300, 70);
label.setBorder(null);
//back and exit buttons
JButton button1 = new JButton("Interchange");
JButton button2 = new JButton ("Intersection");
JButton button3 = new JButton ("Exit");
pane.add(button1);
pane.add(button2);
pane.add(button3);
button1.setBounds(75, 200, 200, 50);
button2.setBounds(75, 270, 200, 50);
button3.setBounds(75, 340, 200, 50);
//add attap label at bottom left
image = new ImageIcon(MainForm.class.getResource("attap_logo.gif"));
label = new JLabel(image);
pane.add(label);
label.setBounds(30, 380, 270, 90);
label.setBorder(null);
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
frame.setVisible(false);
frame.dispose();
InterchangeLoad.main(null);
}
});
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
frame.setVisible(false);
frame.dispose();
MultipleIntersectionLoad.main(null);
}
});
button3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
frame.setVisible(false);
frame.dispose();
}
});
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Critical Lane Volume");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
addComponentsToPane(frame, frame.getContentPane());
frame.setSize(350, 500);
frame.setResizable(false);
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
It looks like one (or more) of the arguments you are passing into your ImageIcon constructor are null.
EDIT
The exception is occurring at one of these two lines:
ImageIcon image = new ImageIcon(MainForm.class.getResource("SHA_logo.gif"));
or
image = new ImageIcon(MainForm.class.getResource("maryland_flag_round.gif"));
In either case, the problem is the same: the resource is not being found, and so null is being returned by getResource(). Why aren't you just using new ImageIcon(String filename)? I'm not even 100% sure what getResource() is doing.
A few other quick comments (suggestions) on your code:
pane.setLayout(null); is not a good idea, I'm not even sure you can just get rid of the LayoutManager like that.
Reusing the same reference (image and label) multiple times for different objects is bad coding style. You're not saving any memory by doing that, and the code makes less sense.
Use more descriptive names! button1 should probably be called 'interchangeButton' or something along those lines. For such a small segment of code, no one will get lost, but if you ever work on a larger project, a good naming scheme is vital.

Categories

Resources