Results 1 to 13 of 13
Like Tree1Likes
  • 1 Post By Tolls

Thread: Why it doesn't work?

  1. #1
    3man75 is offline Member
    Join Date
    Apr 2017
    Posts
    8
    Rep Power
    0

    Default Why it doesn't work?

    So I was working on a personal project of mines when I decided to expand it by creating a Java GUI for it with buttons. I followed the online tutorial of the Newboston in doing this and did everything line for line with the exception of naming the variables differently. It shows no errors and it seems like it should work but it does not.

    Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:217)
    at GUI.<init>(GUI.java:22)
    at Launcher.main(Launcher.java:10)

    Above are the errors I get. I know that the first part has to do with throwing nulls (I think??) but I can't understand where that may be happenig.

    Java Code:
    import java.util.Scanner;
    import javax.swing.JFrame;
    public class Launcher {
    
    	public static void main(String[] args) {
    		Scanner customerInput = new Scanner(System.in);
    		
    		System.out.println("Hello what room would you like?" + "\n1 - Personal \n" + "2 - Room for Two \n"+ "2 - Room for Two \n" + "4 - Group \n");
    		
    		GUI GO = new GUI();
    		GO.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		GO.setSize(300,200);
    		GO.setVisible(true);
    		
    	}
    
    }
    //Make screen appear with options.
    //Click on an option.
    //Display prices.
    //Enter First and Last name followed by Credit Card number.
    // Done.
    Java Code:
    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("Title");
    		setLayout (new FlowLayout());
    		
    		reg = new JButton("Regular Button");
    		add(reg);
    		
    		Icon a =  new ImageIcon(getClass ().getResource("A.JPEG"));
    		Icon b =  new ImageIcon(getClass ().getResource("B.JPEG"));
    		
    		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()));
    		}
    	}
    }
    Finally above is the code I have. I don't know what else to put here to describe the situation as more than what I've written down here.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Why it doesn't work?

    Look at the error message:

    Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:217)
    at GUI.<init>(GUI.java:22) <------------------------------------------here
    at Launcher.main(Launcher.java:10)

    That says that at line 22 a null pointer exception was thrown.

    So break up line 22 and print out the values to see what is returning a null.

    However, why are you using getResource()?

    It would probably be much easier for you to use
    ImageIcon a = new ImageIcon("c:/path/to/image.jpg");

    getResource() is most useful for obtaining bundled stuff in jar files which reside in the class path.

    Oh, and it is rarely (meaning usually never) a good idea to subclass JFrame. Just declare a JFrame instance within
    your class and work with that (and I don't care what the "other" guy did).

    Regards,
    Jim
    Last edited by jim829; 04-29-2017 at 09:30 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    3man75 is offline Member
    Join Date
    Apr 2017
    Posts
    8
    Rep Power
    0

    Default Re: Why it doesn't work?

    Well with some commenting out of some code I can get a window that has regular button and with a popup when I click the popup.

    ImageIcon a = new ImageIcon("c:/path/to/image.jpg");

    Seemed like this piece of code was actually working since the compiler wasn't complaining about it but I can't get the actual picture that I want out.

    C:\Users\Javier\workspace\HotelService\bin\Pictures

    This is where the file with pictures is. It's inside the actual project as you can see.((The bolded part is the project))

    I only have two picutes labeled A and B ((seriously that's what there named)) so how can I show these pictures when I click on the button?

    Also i'm sorry for asking but why shoudn't I just use the extend JFrame? It seems to work fine...what are the drawbacks of using it that way?
    Last edited by 3man75; 04-30-2017 at 08:30 PM.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Why it doesn't work?

    Quote Originally Posted by 3man75 View Post
    Seemed like this piece of code was actually working since the compiler wasn't complaining about it but I can't get the actual picture that I want out.
    Your earlier code was syntactically correct so the compiler didn't complain. But it had execution problems because it could not find the images. Since getResource() returns an instance of URL which in this case is null, the ImageIcon constructor threw an NPE.

    Also i'm sorry for asking but why shoudn't I just use the extend JFrame? It seems to work fine...what are the drawbacks of using it that way?
    There are no specific drawbacks except that it is considered bad style. In fact, it is bad style to subclass anything unless you plan to override existing methods or perhaps add your own to increase functionality. I can't really remember when I needed to subclass JFrame because I never needed to override any methods. I subclass JPanel quite often because I want to override the paintComponent() to do graphics. Subclassing (or inheritance) vs composition. The latter is when you simply use an instance of the class within your class instead of subclassing it.

    Regarding your problem at hand, assuming that you have yet to display the images, I would first focus on just reading and displaying them (don't worry about
    the button stuff). Then once you know the image display code works, you can focus on the other part.

    That is another general programming guideline. Don't try to fix everything at once. Just make certain each piece does what it is supposed to do. Then move
    on to other. Which is why I have lots of little programs exercising specific capabilities of my larger programs. Then when they work, I integrate them together.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    3man75 is offline Member
    Join Date
    Apr 2017
    Posts
    8
    Rep Power
    0

    Default Re: Why it doesn't work?

    I've started over the entire program by doing it again but my own way instead of copying line by line. I can get a frame to appear from the program but I cannot get the darn buttons to do so. I'm doing this as a personal project for learning purposes but I just have to ask: How do I get my buttons to show? An how Can I have text on them?

    I have this so far:

    Java Code:
    public class GUI {
    	
    	private JButton buttonOne;
    	private JButton buttonTwo;
    	private JButton buttonThree;
    	
    	public GUI () {
    		JFrame frame = new JFrame();
    		frame.setTitle("The Begining");
    		frame.setVisible(true);
    		frame.setSize(500, 500);
    		frame.setLocation(0, 0);
    		
    		JButton buttonOne = new JButton();
    		buttonOne = new JButton("Button One");
    		
    	}
    	
    }
    Here is the new class I made for this by the way in full. Everything with frame has to do with that window that pops up and modifies it in some way.

    While on the bottom I first create the object of a button and in the second one I label it as 'Button One'. I assume the second line of code there gives the button that text I was asking about earlier. Except all I get is a blank frame which is depressing to look at without buttons.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Why it doesn't work?

    You didn't add the button to the frame's content pane. So
    Java Code:
    frame.add(buttonOne);
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    3man75 is offline Member
    Join Date
    Apr 2017
    Posts
    8
    Rep Power
    0

    Default Re: Why it doesn't work?

    Quote Originally Posted by jim829 View Post
    You didn't add the button to the frame's content pane. So
    Java Code:
    frame.add(buttonOne);
    Regards,
    Jim
    Java Code:
    public class GUI {
    	
    	private JButton buttonOne;
    	private JButton buttonTwo;
    	private JButton buttonThree;
    	private JPanel panel;
    	private JFrame frame;
    	
    	public GUI () {
    		frame.setTitle("The Begining");
    		frame.setVisible(true);
    		frame.setSize(500, 500);
    		frame.setLocation(0, 0);
    		
    		panel = new JPanel();
    		panel.setBackground(Color.black);
    		
    		
    		buttonOne = new JButton("1 - Personal BedRoom");
    		
    		buttonTwo = new JButton("2 - Two Person Bedroom");
    		
    		buttonThree = new JButton("3 - Group Suite");
    		
    		panel.add(buttonOne);
    		panel.add(buttonTwo);
    		panel.add(buttonThree);
    		frame.add(panel);
    		
    	}
    	
    }
    An almost done!

    So all I need now is the ability to show a price when you click a button and a picture of a generic hotel room. So for price i'm just going to make a generic print statement pop up. But now that of course leads to this question: How can I make a specific picture show up when a button is clicked?

    I know that i'm going to need action listeners but getting those pictures is going to be difficult. Has anyone else done this before?

    I'm thinking of a new class for action listeners so that not everything has to be in my GUI function.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Why it doesn't work?

    Write a scratch class that displays a frame, loads an image and places it in something like a JLabel on the frame.

    That will give you the image loading and displaying code you will eventually need.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    3man75 is offline Member
    Join Date
    Apr 2017
    Posts
    8
    Rep Power
    0

    Default Re: Why it doesn't work?

    Okay for some reason I'm receiving a null point exception.

    Exception in thread "main" java.lang.NullPointerException
    at GUI.<init>(GUI.java:17)
    at main.main(main.java:14)

    What the hell does this mean and what do I do?

    Also @Tolls

    What's a scratch class?

  10. #10
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Why it doesn't work?

    the NPE is fired in the first line of your GUI constructor. If you comment it out it will be fired in the second line of your constructor. And so on for the first 4 lines.

  11. #11
    3man75 is offline Member
    Join Date
    Apr 2017
    Posts
    8
    Rep Power
    0

    Default Re: Why it doesn't work?

    Quote Originally Posted by benji2505 View Post
    the NPE is fired in the first line of your GUI constructor. If you comment it out it will be fired in the second line of your constructor. And so on for the first 4 lines.
    I've seen it before. I've added this line:

    JFrame frame = new JFrame();

    In the constructor and now it works. Why does the IDE do this? I would think that: "private JFrame frame;" would work but it is not so. Why is this?

  12. #12
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Why it doesn't work?

    This is happening because you need an instance of type JFrame in order to work with it. JFrame frame; is just a definition of a reference, but it does not create a new instance of anything.

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Why it doesn't work?

    Quote Originally Posted by 3man75 View Post
    Also @Tolls

    What's a scratch class?
    A throwaway class you can use as a scratchpad for checking code out.
    Mine has a main() method that I use to call one of any number of static methods that do the bit of work I want to check, so in your case you would have something like:

    Java Code:
    public class TestStuff {
        public static void main(String[] args) {
            testImages();
        }
    
        // snip lots of other test methods you may have used before
    
        private static void testImages() {
            // Create frame
            // load image
            // create label with image
            // add to frames content pane
            // display.
        }
    }
    3man75 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Why doesn't this work
    By The Doctor11 in forum New To Java
    Replies: 11
    Last Post: 11-04-2014, 05:57 PM
  2. I wonder why this doesn't work...
    By louboulos in forum New To Java
    Replies: 3
    Last Post: 05-13-2012, 04:59 PM
  3. Why doesn't this work?
    By mailman in forum Java Applets
    Replies: 5
    Last Post: 01-10-2012, 01:01 PM
  4. why this doesn't work?
    By hitesh_public in forum New To Java
    Replies: 5
    Last Post: 08-09-2010, 08:07 AM
  5. Why doesn't this work?
    By Corder10 in forum New To Java
    Replies: 1
    Last Post: 07-04-2009, 10:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •