Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2016
    Location
    Sumner, WA
    Posts
    4
    Rep Power
    0

    Default java.lang.NullPointerException

    So I have looked at this multiple times and I don't quite understand why it is throwing a null error on the object when I reference it to the constructor in the PanelWall class. I have looked over the java docs about the error and narrowed what I think the possibilities of the error are:

    Option 1: Calling the instance method of a null object
    Option 2: Accessing or modifying the field of a null object.
    Both of these relate to a null object but, when I look at my code and run threw it all step by step I take the impression that the object is referencing a private component which is then used in the constructor of the PanelWall class. Also, I created the PanelWall class for the purpose of being able to change the attributes in one area instead of rewriting the code in each line I would have to call it. If there is a more efficient way to go about this please let me know.


    Window.java
    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class Window extends JFrame implements ActionListener{
    	
    	private static final long serialVersionUID = 1L;
    	
    	// Makes 3 button components
    	JButton buttonRelics, buttonNews, buttonExit, buttonMenu;
    	
    	private int width, height;
    	
    	private JPanel panelRelics;
    	private JPanel panelNews;
    	
    	PanelWall pr = new PanelWall(panelRelics);
    	PanelWall pn = new PanelWall(panelNews);
    	
    	public Window(int width, int height){
    		
    		// class variables are equal to the constructors variables in the parameter
    		this.width = width;
    		this.height = height;
    		
    		// Makes a JLabel component and references the image to use as a background
    		JLabel background = new JLabel(new ImageIcon("C:/Users/jo/Documents/eclipse/workspace/HeroSiegeRelics/src/Images/HeroSiegeBackground.png")); 
    		//JLabel HeroSiegeText = new JLabel(new ImageIcon("C:/Users/jo/Documents/eclipse/workspace/HeroSiegeRelics/bin/Images/HeroSiegeText.png"));
    		
    		// Sets the layout for the JLabel background
    		background.setLayout(null);
    		
    		// adds background to JFrame
    		add(background); 
    		
    		// Label bounds
    		background.setBounds(325, 447, 325, 447);
    		
    		
    		// Creates new buttons
    		buttonRelics = new JButton("Relics"); 
    		buttonNews = new JButton("News");
    		buttonExit = new JButton("Exit");
    		buttonMenu = new JButton("Menu");
    		
    		// Sets button size
    		buttonRelics.setPreferredSize(new Dimension(80, 30));
    		buttonNews.setPreferredSize(new Dimension(80, 30));
    		buttonExit.setPreferredSize(new Dimension(80, 30));
    		buttonMenu.setPreferredSize(new Dimension(50, 20));
    		
    		// Sets button positioning  (PosX, PosY, SizeX, SizeY)
    		buttonRelics.setBounds(125, 175, 80, 30);
    		buttonNews.setBounds(125, 225, 80, 30);
    		buttonExit.setBounds(125, 275, 80, 30);
    		buttonMenu.setBounds(0, 0, 80, 30);
    		
    		buttonMenu.setOpaque(false);
    		buttonMenu.setContentAreaFilled(false);
    		buttonMenu.setBorderPainted(true);
    		
    		// Sets text color
    		buttonMenu.setForeground(Color.white);
    		
    		// Adds buttons to label
    		background.add(buttonRelics);
    		background.add(buttonNews);
    		background.add(buttonExit);
    		
    		// Adds ActionListeners and MouseListeners
    		// Menu Actions
    		buttonMenu.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    					background.setVisible(true);
    					//remove(panelWall);
    			}
    		});
    		
    		buttonMenu.addMouseListener(new MouseAdapter(){
    			public void mouseEntered(MouseEvent e){
    				buttonMenu.setToolTipText("Returns to main Menu");
    			}
    			public void mouseExited(MouseEvent e){
    				
    			}
    		});
    		
    		// Relic Actions
    		buttonRelics.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    				background.setVisible(false);
    				add(panelRelics);
    				
    			}
    		});
    		
    		buttonRelics.addMouseListener(new MouseAdapter(){
    			public void mouseEntered(MouseEvent e){
    				buttonRelics.setToolTipText("View all Relics");
    			}
    			public void mouseExited(MouseEvent e){
    				
    			}
    		});
    
    		// News Actions
    		buttonNews.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    				background.setVisible(false);
    				//add(panelWall);
    			}
    		});
    		
    		buttonNews.addMouseListener(new MouseAdapter(){
    			public void mouseEntered(MouseEvent e){
    				buttonNews.setToolTipText("View patch notes");
    			}
    			public void mouseExited(MouseEvent e){
    				
    			}
    		});
    		
    		// Exit 
    		buttonExit.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    				System.exit(0);
    			}
    		});
    		
    		buttonExit.addMouseListener(new MouseAdapter(){
    			public void mouseEntered(MouseEvent e){
    				buttonExit.setToolTipText("Exit application");
    			}
    			public void mouseExited(MouseEvent e){
    				
    			}
    		});
    		
    	}
    
    	// returns width
    	public int getWidth(){
    		return width;
    	}
    
    	// returns height
    	public int getHeight(){
    		return height;
    	}
    
    	
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		
    	}
    }
    
    class PanelWall{
    	
    	private JPanel wall;
    
    	public PanelWall(JPanel panel){
    		wall = panel;
    		wall = new JPanel();
    		wall.setLayout(null);
    		wall.setBackground(Color.black);
    		wall.setBounds(325, 447, 325, 447);
    		
    	}
    }

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

    Default Re: java.lang.NullPointerException

    At lines 27 and 28 you declare panelRelics and panelNews. Then subsequently try and pass them to some methods and use
    them. But they are null as you haven't assigned them any JPanel instances. Passing an empty variable to a class constructor
    or method in Java and changing it in the constructor or method won't change the value. Java, like most other languages,
    passes by value, not by reference.

    Also, you should change your class from Window to something else. Window is an important class in the Java API and your use
    of it could lead to confusion.

    And using absolute positioning like setBounds is considered bad practice. You should use layout managers to organize your components.

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

  3. #3
    Join Date
    Oct 2016
    Location
    Sumner, WA
    Posts
    4
    Rep Power
    0

    Default Re: java.lang.NullPointerException

    Quote Originally Posted by jim829 View Post
    At lines 27 and 28 you declare panelRelics and panelNews. Then subsequently try and pass them to some methods and use
    them. But they are null as you haven't assigned them any JPanel instances. Passing an empty variable to a class constructor
    or method in Java and changing it in the constructor or method won't change the value. Java, like most other languages,
    passes by value, not by reference.
    Is there a tutorial you could reference me to on how to pass a component to a class constructor as I am trying to achieve?

    Quote Originally Posted by jim829 View Post
    Also, you should change your class from Window to something else. Window is an important class in the Java API and your use
    of it could lead to confusion.
    Will do, I will go ahead and change the name of the class

    Quote Originally Posted by jim829 View Post
    And using absolute positioning like setBounds is considered bad practice. You should use layout managers to organize your components.

    Regards,
    Jim
    Yeah, I was looking at Layouts and I found one layout that would prove useful for me but, at this moment I didn't feel comfortable tackling such a task. I know I will need to use it for when I get ready to add the JLabels to the relic as there will be having 199 of them but, for something as minor as the menu I felt it was appropriate. I believe the Layout I would be using the the 199 is java's GridLayout.

    Thank you for your kind words Jim.

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

    Default Re: java.lang.NullPointerException

    You can pass values to your methods like you were doing. But you can't assign a value to a method argument within the method. You can only
    return a value from the method call. Instead of creating a new class, you can do the following:

    Java Code:
    private JPanel panelRelics = createPanel();
    private JPanel panelNews = createPanel();
    Then you can have a static factory method as follows:

    Java Code:
    public static JPanel createPanel() {
            JPanel wall = new JPanel();
            wall.setLayout(null);
            wall.setBackground(Color.black);
            wall.setBounds(325, 447, 325, 447);
            return wall;
    }
    If you need to alter something from one panel to the other, then you can customize the createPanel call
    with arguments.

    You may want to check out the tutorials linked in my signature at the end of this post. They cover lots
    of items including GUI's and layout manager.

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

  5. #5
    Join Date
    Oct 2016
    Location
    Sumner, WA
    Posts
    4
    Rep Power
    0

    Default Re: java.lang.NullPointerException

    I gave you rep. You were an amazing help in understanding why the problem was happening and how to approach fixing the solution and giving feedback on the other code. I have since renamed the class. I just have one more question. This question is related to OOP my Relic Panel is going to contain a lot of labels and for loops and a bunch of other code also consisting of mouse hovers. Should I make a method and just call the method after being clicked or just hard code it into the ActionListener? I feel I should make a method so it doesn't get to clustered inside the ActionListener but, my biggest problem has always been OOP :(

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

    Default Re: java.lang.NullPointerException

    Either way would work. I would use the method approach since it does make the code look cleaner. Be aware that the listeners process events
    on the Event Dispatch Thread. So if you do too much processing say in the ActionListener other GUI features such as mouse movements won't respond.

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

Similar Threads

  1. java.lang.NullPointerException
    By AxL in forum New To Java
    Replies: 1
    Last Post: 12-07-2011, 05:40 AM
  2. Java.Lang.NullPointerException, but I am not sure why???
    By buildakicker in forum New To Java
    Replies: 22
    Last Post: 07-21-2011, 01:20 AM
  3. Replies: 15
    Last Post: 01-13-2011, 06:28 PM
  4. java.lang.NullPointerException
    By peterhabe in forum New To Java
    Replies: 5
    Last Post: 08-27-2010, 08:43 PM
  5. java.lang.NullPointerException
    By stevemcc in forum AWT / Swing
    Replies: 2
    Last Post: 02-08-2008, 09:01 AM

Posting Permissions

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