Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: Null pointer error JButton:

  1. #1
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Null pointer error JButton:

    I am trying to program the A star algorithm using JButtons. Now I have some tweaking to do but I have to get past the errors first. I will post all three classes just because this is my first post with this question on this site. I have asked a similar question on Activity Stream - Java Programming Forum - Learn Java Programming but have been using a past post for for several other topics.

    I am getting the following errors:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at AStar.findEndButton(AStar.java:40)
    	at Screen$1.actionPerformed(Screen.java:59)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    	at java.awt.Component.processMouseEvent(Component.java:6505)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
    	at java.awt.Component.processEvent(Component.java:6270)
    	at java.awt.Container.processEvent(Container.java:2229)
    	at java.awt.Component.dispatchEventImpl(Component.java:4861)
    	at java.awt.Container.dispatchEventImpl(Container.java:2287)
    	at java.awt.Component.dispatchEvent(Component.java:4687)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    	at java.awt.Container.dispatchEventImpl(Container.java:2273)
    	at java.awt.Window.dispatchEventImpl(Window.java:2719)
    	at java.awt.Component.dispatchEvent(Component.java:4687)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    	at java.awt.EventQueue.access$200(EventQueue.java:103)
    	at java.awt.EventQueue$3.run(EventQueue.java:694)
    	at java.awt.EventQueue$3.run(EventQueue.java:692)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    	at java.awt.EventQueue$4.run(EventQueue.java:708)
    	at java.awt.EventQueue$4.run(EventQueue.java:706)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

    In my AStar class you will see these buttons which is an instance of matrixButtons

    MatrixButtons[][] current;
    	MatrixButtons[][] endButton;
    	MatrixButtons[][] startButton;
    	MatrixButtons[][] nextCurrent;
    	MatrixButtons[][] temp;
    	MatrixButtons bestNextMove;

    I really need them to handle several different methods. A full example of the full program is below.

    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.*;
     
    public class Screen 
    {
    	JButton start;
    	JButton reset;
    	static int ROW =20;
    	static int COL =20;
    	MatrixButtons[][] matrixButtons = new MatrixButtons[ROW][COL];
     
    	Screen()
    	{
    		//main frame
    		JFrame j = new JFrame("A* Algorithm");
     
    		//setting main frame
    		j.setSize(1200,500);
    		j.setLayout(new BorderLayout());
    		j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		//create Panel for multi array buttons
    		JPanel p = new JPanel();
    		p.setLayout(new GridLayout(20,20));
     
    		//create command buttons "start" and "restart"
    		JPanel CmdBtns = new JPanel();
     
    		//create buttons matrix of buttons
    		for(int row=0;row<20;row++)
    		{
    			for(int col=0;col<20;col++)
    			{   //create new matrix buttons
    				matrixButtons[row][col] = new MatrixButtons();
    				matrixButtons[row][col].g_value = 0;
    				matrixButtons[row][col].f_value = 0;
    				matrixButtons[row][col].heuristic = 0;
    				//add buttons to panel
    				p.add(matrixButtons[row][col]); 
    			}
    		}
     
    		//create start button
    		start = new JButton("Start");
    		CmdBtns.add(start);
     
    		//handling start button with inner class
    		start.addActionListener(new ActionListener()
    		{
     
    			public void actionPerformed(ActionEvent ae) 
    			{
    				AStar star =new AStar();
     
    				star.findEndButton();
    				star.findStartButton();
    				star.findPath();
    				star.markPath();
    			}
     
    		});
     
    		//create reset button
    		reset = new JButton("Reset");
    		CmdBtns.add(reset);
     
    		//handling reset button with inner class
    		reset.addActionListener(new ActionListener()
    		{
     
    			public void actionPerformed(ActionEvent ae) 
    			{
    				for(int i=0; i<ROW;i++)
    				{
    					for(int j=0; j<COL; j++)
    					{
    						matrixButtons[i][j].setIcon(null);
    					}
    				}
    			}
     
    		});
     
    				//Centering matrix buttons onto main JFrame
    				j.add(p,BorderLayout.CENTER);
     
    				//adding start and restart buttons
    				j.add(CmdBtns,BorderLayout.SOUTH);
    				j.setVisible(true);
    	} 
    }

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
     
    public class MatrixButtons extends JButton implements ActionListener
    {
    	//Icons to be displayed in box[][]
    		ImageIcon wall; 
    		ImageIcon begin; 
    		ImageIcon end; 
     
    		int f_value; // G + heuristic
    		int g_value; //movement cost
    		int heuristic; //distance from current node to end node
     
    		byte value =0;
    		/*
    		 0:nothing
    		 1:wall
    		 2:begin
    		 3:end
    		 */
     
    		MatrixButtons()
    		{
    			wall = new ImageIcon("wall.png");
    			begin = new ImageIcon("begin.png");
    			end = new ImageIcon("end.png");
    			this.addActionListener(this);
    		}
     
    		public void actionPerformed(ActionEvent ae) 
    		{
    			value++;
    			value%=4;//resets button after pressed 4 times
     
    			switch(value)
    			{
    			case 0:
    				//button has not been pressed
    				setIcon(null);   
    				break;
    			case 1:
    				//button has been pressed once
    				setIcon(wall); 
    				break;
    			case 2:
    				//button has been pressed twice
    				setIcon(begin);   
    				break;
    			case 3:
    				//button has been pressed three times
    				setIcon(end);
    				break;
    				default:
    					break;
    			}
     
    		}
     
    }

    import java.awt.BorderLayout;
    import java.util.ArrayList;
     
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
     
    public class AStar extends MatrixButtons
    {
     
    	ArrayList<MatrixButtons> openList = new ArrayList<MatrixButtons>();
    	ArrayList<MatrixButtons> tempList = new ArrayList<MatrixButtons>();
    	ArrayList<MatrixButtons> closedList = new ArrayList<MatrixButtons>();
     
    	MatrixButtons[][] current;
    	MatrixButtons[][] endButton;
    	MatrixButtons[][] startButton;
    	MatrixButtons[][] nextCurrent;
    	MatrixButtons[][] temp;
    	MatrixButtons bestNextMove;
    	//stores the location of the next best move in order to find it
    	//inside tempList from another method
    	int nextMoveIndex; 
     
    	//these variables will be used to find the rows and columns
    	//in order to get the heuristic
    	int startRow;
    	int startCol;
    	int endRow;
    	int endCol;
     
    	//loop though all buttons and finds the end icon
    	void findEndButton()
    	{
     
    		for(int i=0;i<20;i++)
    		{
    			for(int j =0;j<20;j++)
    			{
    				if(temp[i][j].getIcon()!= null)
    				{
    				//find end icon and setting it to endButton
    				if(temp[i][j].getIcon().equals(end))
    				{
    					endButton[i][j]=temp[i][j];
    					endRow = i;//will be used to find the heuristic
    					endCol = j;//will be used to find the heuristic
    				}
    				}
    			}
    		}
    	}
    	//loops through all the buttons and finds the start icon
    	void findStartButton()
    	{
    		for(int i=0;i<20;i++)
    		{
    			for(int j =0;j<20;j++)
    			{
    				if(temp[i][j].getIcon() != null)
    				{
    				//find start icon and setting it to startButton
    				if(temp[i][j].getIcon().equals(begin))
    				{
    					startButton[i][j]=temp[i][j];
    					startRow = i;//will be used to find the heuristic
    					startCol = j;//will be used to find the heuristic
    				}
    				}
    			}
    		}
    	}
     
    	/* 
    	 if movement is diagonal g_value is set to 15
    	 else it is vertical and set to 10
    	 This method gets the current nodes row and column and
    	 sets the g_value instead of using a loop 
    	 */
    	void setValues(int row, int col)
    	{
    		//diagonal top left
    		current[row-1][col-1].g_value=15;
    		current[row-1][col-1].heuristic = getHeuristic(current,row-1,col-1);
    		current[row-1][col-1].f_value = getFvalue(current,row-1,col-1);
    		//diagonal top right
    		current[row-1][col+1].g_value=15;
    		current[row-1][col+1].heuristic = getHeuristic(current,row-1,col+1);
    		current[row-1][col+1].f_value = getFvalue(current,row-1,col+1);
    		//diagonal bottom right
    		current[row+1][col+1].g_value=15;
    		current[row+1][col+1].heuristic = getHeuristic(current,row+1,col+1);
    		current[row+1][col+1].f_value = getFvalue(current,row+1,col+1);
    		//diagonal bottom left
    		current[row+1][col-1].g_value=15;
    		current[row+1][col-1].heuristic = getHeuristic(current,row+1,col-1);
    		current[row+1][col-1].f_value = getFvalue(current,row+1,col-1);
     
    		//vertical top
    		current[row-1][col].g_value=10;
    		current[row-1][col].heuristic = getHeuristic(current,row-1,col);
    		current[row-1][col].f_value = getFvalue(current,row-1,col);
    		//vertical right
    		current[row][col+1].g_value=10;
    		current[row][col+1].heuristic = getHeuristic(current,row,col+1);
    		current[row][col+1].f_value = getFvalue(current,row,row+1);
    		//vertical bottom
    		current[row+1][col].g_value=10;
    		current[row+1][col].heuristic = getHeuristic(current,row+1,col);
    		current[row+1][col].f_value = getFvalue(current,row+1,col);
    		//vertical left
    		current[row][col-1].g_value=10;
    		current[row][col-1].heuristic = getHeuristic(current,row,col-1);
    		current[row][col-1].f_value = getFvalue(current,row,col-1);
     
    	}
    	/*
    	 Total distance from start button to end button. Walls
    	 will not be considered as barriers.
    	 */
    	int getHeuristic(MatrixButtons[][] h, int row, int col)
    	{
    		return h[row][col].heuristic = (endRow-row) + (endCol-col);
    	}
     
    	/*
    	 getFvalue returns getHeuristic() + getGvalue() to later decide
    	 the best move to make next
    	 */
    	int getFvalue(MatrixButtons[][] f,int row, int col)
    	{
    		return f[row][col].f_value = f[row][col].g_value 
    			   + getHeuristic(f,row,col);	
    	}
     
    	/*this method will receive the row and column of the current button
    	  and return a matrixButton with a lesser f_value by placing each option
    	  on the ArrayList openList, sorting openList least to greatest, and
    	  returning the first matrixButton on the list. The rest of the buttons will
    	  be moved to the closed list so they are never searched again and the
    	  open listed is cleared.
     
    	  */
    	MatrixButtons findNextMove(int row, int col)
    	{
    		int value=0;
    		int temp=0;
     
    		//adding elements on the temp list to be checked
    		//diagonal top left
    		tempList.add(current[row-1][col-1]);
    		//diagonal top right
    		tempList.add(current[row-1][col+1]);
    		//diagonal bottom right
    		tempList.add(current[row+1][col+1]);
    		//diagonal bottom left
    		tempList.add(current[row+1][col-1]);
    		//vertical top
    		tempList.add(current[row-1][col]);
    		//vertical right
    		tempList.add(current[row][col+1]);
    		//vertical bottom
    		tempList.add(current[row+1][col]);
    		//vertical left
    		tempList.add(current[row][col-1]);
     
    		//setting temp to the first number
    		//assuming it is the smallest number
    		temp = tempList.get(0).f_value;
     
    		for(int i=0; i<tempList.size();i++)
    		{
    			value = tempList.get(i).f_value;
     
    			if(temp > value)
    			{
    				//temp is not equal to the smallest number again
    				temp=value;
    				bestNextMove = tempList.get(i);
    				nextMoveIndex =i;
    			}
     
    		}
    		//returns the button with the smallest f_value
    		return bestNextMove; 
    	}
     
    	//puts checked and unused buttons onto closed list
    	// and keeps used button on closed list.
    	void cleanTempList()
    	{
    		//moves the bestNextMove into the open list
    		openList.add(tempList.get(nextMoveIndex));
     
    		//removes the bestNextMove from tempList
    		tempList.remove(nextMoveIndex);
     
    		//store remaining buttons that were searched and
    		//found not to be the shortest path to closed List
    		for(int i=0;i<tempList.size();i++)
    		{
    			closedList.set(i, tempList.get(i));
    		}
     
    		//clears tempList
    		tempList.clear();
     
    	}
     
    	void findPath()
    	{
    		//setting current button to start button
    		current = startButton;
    		JLabel found;
    		JPanel panel;
     
    		int row = startRow;
    		int col = startCol;
     
    		//setting g_value,and Heuristic to start buttons position
    		setValues(row,col);
     
    		while(nextCurrent != endButton)
    		{
    			nextCurrent[row][col]=findNextMove(row,col);
     
    			if(nextCurrent==endButton)
    			{
    				JFrame j = new JFrame("FOUND!!!");
    				panel = new JPanel();
    				panel.setLayout(new BorderLayout());
    				found = new JLabel("END BUTTON WAS FOUND!!!");
    				panel.add(found);
    				j.add(panel);
    				j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				j.setVisible(true);
    			}
    			//resetting tempList and putting values checked value that were
    			//not used onto closed list
    			cleanTempList();
     
    			//recursion
    			findPath();
    		}
     
    	}
     
    	void markPath()
    	{
    		for(int i=0;i<openList.size();i++)
    		{
    		openList.get(i).setIcon(begin);
    		}
    	}
    	public static void main(String[] args)
    	{
    		new Screen();	
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Null pointer error JButton:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at AStar.findEndButton(AStar.java:40)
    What variable on line 40 has the null value? Find the variable and then backtrack to find out why it does not have a valid value.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Null pointer error JButton:

    it is this method:

    void findEndButton()
    	{
     
    		for(int i=0;i<20;i++)
    		{
    			for(int j =0;j<20;j++)
    			{
    				if(temp[i][j].getIcon()!= null)
    				{
    				//find end icon and setting it to endButton
    				if(temp[i][j].getIcon().equals(end))
    				{
    					endButton[i][j]=temp[i][j];
    					endRow = i;//will be used to find the heuristic
    					endCol = j;//will be used to find the heuristic
    				}
    				}
    			}
    		}
    	}

    From these variables:

    MatrixButtons[][] current;
    	MatrixButtons[][] endButton;
    	MatrixButtons[][] startButton;
    	MatrixButtons[][] nextCurrent;
    	MatrixButtons[][] temp;
    	MatrixButtons bestNextMove;

    The above is null, and I need to initialize them I suppose so they are not null but to what?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Null pointer error JButton:

    I need to initialize them I suppose so they are not null but to what?
    That is up to the programmer to decide what value to put into a variable. What are those variables supposed to hold? When you know that, then you will know what to assign to them.

    This line from another class assigns a value:
    	MatrixButtons[][] matrixButtons = new MatrixButtons[ROW][COL];
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Null pointer error JButton:

    I have tried this before and got the same errors. I know what I need to use them for and I am. I am calling the class and creating a new object

     
    	ArrayList<MatrixButtons> openList = new ArrayList<MatrixButtons>();
    	ArrayList<MatrixButtons> tempList = new ArrayList<MatrixButtons>();
    	ArrayList<MatrixButtons> closedList = new ArrayList<MatrixButtons>();
     
    	MatrixButtons[][] current = new MatrixButtons[20][20];
    	MatrixButtons[][] endButton = new MatrixButtons[20][20];
    	MatrixButtons[][] startButton= new MatrixButtons[20][20];
    	MatrixButtons[][] nextCurrent= new MatrixButtons[20][20];
    	MatrixButtons[][] temp= new MatrixButtons[20][20];

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Null pointer error JButton:

    got the same errors
    What was the code that got the error?
    When working with arrays, there are several steps needed:
    1) define the variable
    2) assign to the variable an array of slots to hold values
    3) assign a value to each of the slots in the array.

    Did the code do all three steps? A slot in the array without a value assigned to it will be null.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Null pointer error JButton:

    	//initialize new buttons
    	void initialize()
    	{
    		for(int i=0;i<20;i++)
    		{
    			for(int j=0;j<20;j++)
    			{
    				current[i][j] = new MatrixButtons();
    				endButton[i][j]=new MatrixButtons();
    				startButton[i][j]=new MatrixButtons();
    				nextCurrent[i][j]=new MatrixButtons();
    				temp[i][j]=new MatrixButtons();
    			}
    		}
    	}

    Thanks Norm

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Null pointer error JButton:

    How many hundreds of times are the ImageIcons being loaded now?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Null pointer error JButton:

    I was having a hard time trying to figure out a way to handle this function so I got out a piece of paper and got this logic below. However I would prefer to pass in a button and get is current position and do the following. Here is the next issue, several lines could be out of bounds depending on where the current[][] button it.

    Any advice?

    void setValues(int row, int col)
    	{
    		//diagonal top left
    		current[row-1][col-1].g_value=15;
    		current[row-1][col-1].heuristic = getHeuristic(current,row-1,col-1);
    		current[row-1][col-1].f_value = getFvalue(current,row-1,col-1);
    		//diagonal top right
    		current[row-1][col+1].g_value=15;
    		current[row-1][col+1].heuristic = getHeuristic(current,row-1,col+1);
    		current[row-1][col+1].f_value = getFvalue(current,row-1,col+1);
    		//diagonal bottom right
    		current[row+1][col+1].g_value=15;
    		current[row+1][col+1].heuristic = getHeuristic(current,row+1,col+1);
    		current[row+1][col+1].f_value = getFvalue(current,row+1,col+1);
    		//diagonal bottom left
    		current[row+1][col-1].g_value=15;
    		current[row+1][col-1].heuristic = getHeuristic(current,row+1,col-1);
    		current[row+1][col-1].f_value = getFvalue(current,row+1,col-1);
     
    		//vertical top
    		current[row-1][col].g_value=10;
    		current[row-1][col].heuristic = getHeuristic(current,row-1,col);
    		current[row-1][col].f_value = getFvalue(current,row-1,col);
    		//vertical right
    		current[row][col+1].g_value=10;
    		current[row][col+1].heuristic = getHeuristic(current,row,col+1);
    		current[row][col+1].f_value = getFvalue(current,row,row+1);
    		//vertical bottom
    		current[row+1][col].g_value=10;
    		current[row+1][col].heuristic = getHeuristic(current,row+1,col);
    		current[row+1][col].f_value = getFvalue(current,row+1,col);
    		//vertical left
    		current[row][col-1].g_value=10;
    		current[row][col-1].heuristic = getHeuristic(current,row,col-1);
    		current[row][col-1].f_value = getFvalue(current,row,col-1);
     
    	}


    --- Update ---

    How many hundreds of times are the ImageIcons being loaded now?
    I need those variables....

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Null pointer error JButton:

    I need those variables....
    The images only need to be read one time. Then they can be passed to the MatrixButtons class in its constructor and saved in the class.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Null pointer error JButton:

    MatrixButtons()
    		{
    			wall = new ImageIcon("wall.png");
    			begin = new ImageIcon("begin.png");
    			end = new ImageIcon("end.png");
    			this.addActionListener(this);
    		}

    I did

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Null pointer error JButton:

    I did
    What did you change? It looks like the same code that has the problem.
    Add a println() statement next to those statements and see how many times it prints on the console.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Null pointer error JButton:

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
     
    public class MatrixButtons extends JButton implements ActionListener
    {
    	//Icons to be displayed in box[][]
    		ImageIcon wall = new ImageIcon("wall.png");
    		ImageIcon begin = new ImageIcon("begin.png");
    		ImageIcon end = new ImageIcon("end.png"); 
     
    		int f_value; // G + heuristic
    		int g_value; //movement cost
    		int heuristic; //distance from current node to end node
     
    		byte value =0;
    		/*
    		 0:nothing
    		 1:wall
    		 2:begin
    		 3:end
    		 */
     
    		MatrixButtons()
    		{
    			//wall = new ImageIcon("wall.png");
    			//begin = new ImageIcon("begin.png");
    			//end = new ImageIcon("end.png");
    			//this.addActionListener(this);
    		}

    Now it will not work. I get what you are saying norm. Maybe this is the issue, I am thinking of each button as nodes. As if I was using a inner class so therefore I want each node(button) to have the appropriate variables. Do you have ay advice about post 9

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Null pointer error JButton:

    Load the images one time and pass the references to the images in the constructor:
    ImageIcon xC = new ImageIcon("asd.png");  // load icon one time here
      ...
    for(int i=0; i < array.length; i++ ) {
       array[i] = new SomeClass(xC);  // create instance and pass reference to constructor
    }
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Null pointer error JButton:

    ahhhhhhhhhhhhhh........

  16. #16
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Null pointer error JButton:

    This is causing me several issues norm. I decided I would create the buttons in the screen class, makes sense right. I need each button to have 3 icons set to it.


    Screen()
    	{
    		//main frame
    		JFrame j = new JFrame("A* Algorithm");
     
    		//setting main frame
    		j.setSize(1200,500);
    		j.setLayout(new BorderLayout());
    		j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		//create Panel for multi array buttons
    		JPanel p = new JPanel();
    		p.setLayout(new GridLayout(20,20));
     
    		//create command buttons "start" and "restart"
    		JPanel CmdBtns = new JPanel();
     
    		//create buttons matrix of buttons
    		for(int row=0;row<20;row++)
    		{
    			for(int col=0;col<20;col++)
    			{   
    				//create new matrix buttons
    				//set icons to each button
    				matrixButtons[row][col]= new MatrixButtons(wall);
    				matrixButtons[row][col]= new MatrixButtons(begin);
    				matrixButtons[row][col]= new MatrixButtons(end);
     
    				matrixButtons[row][col].g_value = 0;
    				matrixButtons[row][col].f_value = 0;
    				matrixButtons[row][col].heuristic = 0;
    				//add buttons to panel
    				p.add(matrixButtons[row][col]); 
    			}
    		}

    Now I have tried a few other things but this all reverts back to my other solution. EVERY button MUST have three variables, and three
    images inside of it. If I do it your way I will have to send it an image every time I want to create a new button.

    Now this fails but I need initialize buttons here.
                                    current[i][j] = new MatrixButtons();
    				endButton[i][j]=new MatrixButtons();
    				startButton[i][j]=new MatrixButtons();
    				nextCurrent[i][j]=new MatrixButtons();
    				temp[i][j]=new MatrixButtons();

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
     
    public class MatrixButtons extends JButton implements ActionListener
    {
     
    		int f_value; // G + heuristic
    		int g_value; //movement cost
    		int heuristic; //distance from current node to end node
     
    		byte value =0;
    		/*
    		 0:nothing
    		 1:wall
    		 2:begin
    		 3:end
    		 */
     
    		//Icons to be displayed in box[][]
    		ImageIcon wall;//= new ImageIcon("wall.png");
    		ImageIcon begin;//= new ImageIcon("begin.png"); 
    		ImageIcon end; //= new ImageIcon("end.png");
     
    		MatrixButtons()
    		{
    			wall = new ImageIcon("wall.png");
    			begin = new ImageIcon("begin.png");
    			end = new ImageIcon("end.png");
    			this.addActionListener(this);
    		}
     
    		public void actionPerformed(ActionEvent ae) 
    		{
    			value++;
    			value%=4;//resets button after pressed 4 times
     
    			switch(value)
    			{
    			case 0:
    				//button has not been pressed
    				setIcon(null);   
    				break;
    			case 1:
    				//button has been pressed once
    				setIcon(wall); 
    				break;
    			case 2:
    				//button has been pressed twice
    				setIcon(begin);   
    				break;
    			case 3:
    				//button has been pressed three times
    				setIcon(end);
    				break;
    				default:
    					break;
    			}
     
    		}
     
    }

    I am not sure if you been really reading the whole code but as you can see what the current setting of each button it extremely important. The whole logic of the program is set around it. Every button must be treated as a node and the program will find the end icon that is set from the start icon that is set. It will ignore all wall icons that are set and go around them using the variables inside of each button to decide the direction.

    Any advice for post #9.
    Last edited by jocdrew21; May 31st, 2014 at 04:08 AM.

  17. #17
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Null pointer error JButton:

    Can wall, begin, and end be static final class variables WALL, BEGIN, and END?

    As for your post #9 question, you should check that the (row, col) coordinates for each expression are within the array's boundaries. If not, skip that expression.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Null pointer error JButton:

    If I do it your way I will have to send it an image every time I want to create a new button.
    It is possible to pass more that one image in the constructor:
    = new YourConstructor(image1, image2, image3, image4, image5); // pass 5 images to the constructor
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Null Pointer Exception error
    By Keitho55 in forum Exceptions
    Replies: 3
    Last Post: November 18th, 2011, 03:29 AM
  2. NULL POINTER EXCEPTION error
    By beefwithrice in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 28th, 2011, 06:26 AM
  3. Array of strings Null Pointer error
    By FredrichGauss in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 3rd, 2011, 12:43 AM
  4. JComboBox null pointer Exception error
    By F_Arches in forum AWT / Java Swing
    Replies: 2
    Last Post: November 29th, 2009, 02:32 PM
  5. Getting Null Pointer Exception in runtime
    By RoadRunner in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2009, 01:21 PM