Page 1 of 2 12 LastLast
Results 1 to 20 of 21
Like Tree1Likes

Thread: NullPointerException Help!

  1. #1
    amazin112 is offline Member
    Join Date
    Apr 2012
    Posts
    10
    Rep Power
    0

    Default NullPointerException Help!

    The program compiles fine and there are no errors or warnings. When I try to select the buyCheapTreat button I get a runtime NullPointerException error.

    Java Code:
    //When the button is clicked, the ActionListener implementation calls Owner.buyCheapTreat().
    	private class buyCheapTreatListener implements ActionListener{
    		public void actionPerformed(ActionEvent e){
    			theOwner.buyCheapTreat("Cheap Treat");
    		}
    	}
    Java Code:
    /*buyCheapTreat(String)
    	 *If the Owner can afford the CheapTreat then Create a
    	 *cheap treat with the given description string and
    	 *adjust the credit count.
    	 */
    	public CheapTreat buyCheapTreat(String descrip){
    		CheapTreat aTreat = new CheapTreat(descrip);
    		if(credits >= aTreat.getCost()){
    			adjustCredits(-aTreat.getCost());
    			addTreat(aTreat);
    			return aTreat;
    		}
    		else{
    			return null;
    		}
    	}

  2. #2
    amazin112 is offline Member
    Join Date
    Apr 2012
    Posts
    10
    Rep Power
    0

    Default Re: NullPointerException Help!

    Sorry Line4 theOwner.buyCheapTreat("Cheap Treat"); is the one that is causing the runtime error. I'm not sure how to adjust the code to rectify this.

  3. #3
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: NullPointerException Help!

    Can you post the entire stack trace? What it prints out when it gives you the error. It sounds like it's trying to reference something that is not there.

  4. #4
    amazin112 is offline Member
    Join Date
    Apr 2012
    Posts
    10
    Rep Power
    0

    Default Re: NullPointerException Help!

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at view.ICritterView$buyFancyTreatListener.actionPerf ormed(ICritterView.java:121)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2028)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2351)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6375)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:614 0)
    at java.awt.Container.processEvent(Container.java:208 3)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4737)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2141)
    at java.awt.Component.dispatchEvent(Component.java:45 65)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4619)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4280)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4210)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2127)
    at java.awt.Window.dispatchEventImpl(Window.java:2482 )
    at java.awt.Component.dispatchEvent(Component.java:45 65)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:684)
    at java.awt.EventQueue.access$000(EventQueue.java:85)
    at java.awt.EventQueue$1.run(EventQueue.java:643)
    at java.awt.EventQueue$1.run(EventQueue.java:641)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$2.run(EventQueue.java:657)
    at java.awt.EventQueue$2.run(EventQueue.java:655)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 654)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)

    Line 121 in the code is line 4 referenced above.

  5. #5
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: NullPointerException Help!

    Ok, Can you post the rest of the code? I want to see where it is going wrong and run it for myself.

  6. #6
    amazin112 is offline Member
    Join Date
    Apr 2012
    Posts
    10
    Rep Power
    0

    Default Re: NullPointerException Help!

    I would probably need to email it to you, it is 10 classes all together.
    Here's a dropbox link: https://www.dropbox.com/sh/f3swcq89823mc5o/NvdjSWrKXj
    Last edited by amazin112; 04-29-2012 at 06:54 AM.

  7. #7
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: NullPointerException Help!

    Since I can't get it to run. I can only offer you my best guess.

    Java Code:
    public FancyTreat buyFancyTreat(String descrip){
    		FancyTreat aTreat = new FancyTreat(descrip);
    		if(credits >= aTreat.getCost()){
    			adjustCredits(-aTreat.getCost());
    			addTreat(aTreat);
    			return aTreat;
    		}
    		else{
    			return null;
    		}
    	}
    What is the value of credits when you are calling this. The fancy treats costs 5. If you can't afford it, it will return a null. This could be why you are getting your null reference.

  8. #8
    amazin112 is offline Member
    Join Date
    Apr 2012
    Posts
    10
    Rep Power
    0

    Default Re: NullPointerException Help!

    You couldn't compile the files I attached?

    Here's the section of code where credits is defined and initialized:
    Java Code:
    public class Owner extends Observable{
    	
    	private ICritter iCritter;
    	private List<Treat> treats;
    	private Integer credits;
    	private String name;
    	
    	
    	/*The Owner constructor should create an iCritter 
    	 * and initialize the credit count to 30 credits.
    	 */
    	public Owner(String ownerName, String critterName){
    		name = ownerName;
    		iCritter = new ICritter(critterName, this);
    		credits = 30;
    		treats = new ArrayList<Treat>();	
    	}
    Credits should be initialized as 30.

  9. #9
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: NullPointerException Help!

    I couldn't get the class ICritterController to run. It has the main method, so I figured it was the one I should call. I also couldn't compile some of the classes. I'll give it another shot. You made an instance of Owner, but you never initialized it.

    Java Code:
    Owner theOwner;
    That may be your null value.

  10. #10
    amazin112 is offline Member
    Join Date
    Apr 2012
    Posts
    10
    Rep Power
    0

    Default Re: NullPointerException Help!

    That's exactly what the error was... Thank you for your help.

    Here's how I changed the code to fix it:
    Java Code:
    Owner theOwner = new Owner("", "");

  11. #11
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: NullPointerException Help!

    That works, although you may lose any information on it. You passed an Owner object to the constructor of ICritterView. All you have to do is assign it to the theOwner variable.

    Java Code:
    public ICritterView(Owner theOwner, ICritter theCritter){
    		 this.theOwner = theOwner; 
    		setLayout(new BorderLayout());
    
    }
    That way your not making a blank Owner object.


    This whole thing was a little above my coding abilities, but I worked through it the best I could.
    Last edited by Wnt2bsleepin; 04-29-2012 at 08:21 AM.

  12. #12
    amazin112 is offline Member
    Join Date
    Apr 2012
    Posts
    10
    Rep Power
    0

    Default Re: NullPointerException Help!

    You are right, making the blank Owner object doesn't only gets rid of the NPE it doesn't solve the problem. However when I try your solution of this.theOwner = theOwner; I am still getting the NPE error. Does this mean that something else is giving a null value?

  13. #13
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: NullPointerException Help!

    Quote Originally Posted by amazin112 View Post
    You are right, making the blank Owner object doesn't only gets rid of the NPE it doesn't solve the problem. However when I try your solution of this.theOwner = theOwner; I am still getting the NPE error. Does this mean that something else is giving a null value?
    Could be my solution as well. I would also go back to make sure you didn't forget to assign an object somewhere. You can try changing the field theOwner to another name, that way you could do

    Java Code:
    LeOwner = theOwner;
    See if that still throws a null.

  14. #14
    amazin112 is offline Member
    Join Date
    Apr 2012
    Posts
    10
    Rep Power
    0

    Default Re: NullPointerException Help!

    Here's the updated stack trace, I have also updated the dropbox file above in case the line numbers have changed since I have been messing with the code.

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at view.ICritterView.updateCredits(ICritterView.java: 232)
    at view.ICritterView.updateOwnerComponents(ICritterVi ew.java:222)
    at view.ICritterView.update(ICritterView.java:204)

    at java.util.Observable.notifyObservers(Observable.ja va:142)
    at model.Owner.adjustCredits(Owner.java:30)
    at model.Owner.buyFancyTreat(Owner.java:74)
    at view.ICritterView$buyFancyTreatListener.actionPerf ormed(ICritterView.java:126)

    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2028)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2351)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6375)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:614 0)
    at java.awt.Container.processEvent(Container.java:208 3)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4737)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2141)
    at java.awt.Component.dispatchEvent(Component.java:45 65)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4619)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4280)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4210)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2127)
    at java.awt.Window.dispatchEventImpl(Window.java:2482 )
    at java.awt.Component.dispatchEvent(Component.java:45 65)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:684)
    at java.awt.EventQueue.access$000(EventQueue.java:85)
    at java.awt.EventQueue$1.run(EventQueue.java:643)
    at java.awt.EventQueue$1.run(EventQueue.java:641)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$2.run(EventQueue.java:657)
    at java.awt.EventQueue$2.run(EventQueue.java:655)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 654)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)

  15. #15
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: NullPointerException Help!

    Quote Originally Posted by amazin112 View Post
    Here's the updated stack trace, I have also updated the dropbox file above in case the line numbers have changed since I have been messing with the code.

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at view.ICritterView.updateCredits(ICritterView.java: 232)
    at view.ICritterView.updateOwnerComponents(ICritterVi ew.java:222)
    at view.ICritterView.update(ICritterView.java:204)

    at java.util.Observable.notifyObservers(Observable.ja va:142)
    at model.Owner.adjustCredits(Owner.java:30)
    at model.Owner.buyFancyTreat(Owner.java:74)
    at view.ICritterView$buyFancyTreatListener.actionPerf ormed(ICritterView.java:126)

    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2028)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2351)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6375)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:614 0)
    at java.awt.Container.processEvent(Container.java:208 3)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4737)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2141)
    at java.awt.Component.dispatchEvent(Component.java:45 65)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4619)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4280)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4210)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2127)
    at java.awt.Window.dispatchEventImpl(Window.java:2482 )
    at java.awt.Component.dispatchEvent(Component.java:45 65)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:684)
    at java.awt.EventQueue.access$000(EventQueue.java:85)
    at java.awt.EventQueue$1.run(EventQueue.java:643)
    at java.awt.EventQueue$1.run(EventQueue.java:641)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$2.run(EventQueue.java:657)
    at java.awt.EventQueue$2.run(EventQueue.java:655)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 654)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    What are you passing adjCredits through as when you get this error? Are you passing anything at all?

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: NullPointerException Help!

    You're not debugging your NPE correctly. Look at the *first line* in the stacktrace that references your class code:

    Java Code:
    at view.ICritterView.updateCredits(ICritterView.java:232)
    That line, ICritterView.java, line 232, is the one with the null reference.

  17. #17
    amazin112 is offline Member
    Join Date
    Apr 2012
    Posts
    10
    Rep Power
    0

    Default Re: NullPointerException Help!

    Quote Originally Posted by Wnt2bsleepin View Post
    What are you passing adjCredits through as when you get this error? Are you passing anything at all?
    I took that out and just added it to the method above it, seemed unnecessary. I'm going to just post the two classes in here to see if anyone has any ideas. I'm at a loss.

    Here's the ICritterView.java
    Java Code:
    package view;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import model.*;
    
    
    public class ICritterView extends JPanel implements Observer{
    	
    	private static final long serialVersionUID = 1L;
    	
    	Owner theOwner;
    	ICritter theCritter;
    	JList treatsList, memoriesList;
    	JButton giveTreatButton, buyCheapTreatButton, buyFancyTreatButton;
    	JLabel creditsLabel, numCreditsLabel;
    	DefaultListModel treatsListModel, memoriesListModel;
    	JPanel ownerPanel, critterPanel;
    	JSlider happinessSlider;
    
    	/*This method creates all of the Swing components needed and combines 
    	 *them into the GUI. It should call updateiCritterComponents()and 
    	 *updateOwnerComponents() to the appropriate components' values.
    	 */
    	public ICritterView(Owner theOwner, ICritter theCritter){
    		
    		this.theOwner = theOwner;
    		
    		theOwner.addObserver(this);
    		theOwner.getCritter().addObserver(this);
    		
    		setLayout(new BorderLayout());
    		
    		/*The JPanel that contains the Owner-related components should have
    		 *a GridLayout layout manager with three rows and two columns. 
    		 */
    		JPanel ownerPanel = new JPanel();
    		ownerPanel.setLayout(new GridLayout(3,2));
    		add(ownerPanel, BorderLayout.SOUTH);
    		
    		//The first JLabel should contain the String "Credits:"
    		JLabel creditsLabel = new JLabel("Credits:");
    		ownerPanel.add(creditsLabel);
    		
    		//The second JLabel should contain a String with the number of credits the Owner has.
    		JLabel numCreditsLabel = new JLabel(""+theOwner.getCredits());
    		ownerPanel.add(numCreditsLabel);
    		
    		/*The third entry should be a JList, and should list all of the treats 
    		 *the Owner currently has (you can use a ListModel to deal with the 
    		 *elements of the JList).
    		 */
    		treatsListModel = new DefaultListModel();
    		treatsList = new JList(treatsListModel);
    		treatsList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
    		treatsList.setAutoscrolls(true);
    		treatsList.setPreferredSize(new Dimension(150, 100));
    		setupTreatsList(theOwner.listTreats());
    		ownerPanel.add(treatsList);
    		
    		/*The fourth entry should be a JButton with the label "Give Treat".
    		 *This button should have an ActionListener implementation added to it.
    		 */
    		giveTreatButton = new JButton("Give Treat");
    		giveTreatButton.addActionListener(new giveTreatListener());
    		ownerPanel.add(giveTreatButton);
    		
    		/*The fifth entry should be a JButton with the label “Buy Fancy Treat.” 
    		 *This button should have an ActionListener implementation added to it.
    		 */
    		buyFancyTreatButton = new JButton("Buy Fancy Treat");
    		buyFancyTreatButton.addActionListener(new buyFancyTreatListener());
    		ownerPanel.add(buyFancyTreatButton);
    		
    		/*Finally, the sixth entry should be a JButton with the label “Buy Cheap Treat.” 
    		 *This button should have an ActionListener implementation added to it.
    		 */
    		buyCheapTreatButton = new JButton("Buy Cheap Treat");
    		buyCheapTreatButton.addActionListener(new buyCheapTreatListener());
    		ownerPanel.add(buyCheapTreatButton);
    		
    		//The JPanel containing the ICritter-related components should have a BorderLayout layout manager.
    		JPanel critterPanel = new JPanel();
    		critterPanel.setLayout(new BorderLayout());
    		add(critterPanel, BorderLayout.EAST);
    		
    		//It should contain a JLabel at BorderLayout.NORTH containing the string “Happiness and Memories".
    		JLabel handm = new JLabel("Happiness and Memories");
    		critterPanel.add(handm, BorderLayout.NORTH);
    		
    		/*It should also contain a JSlider at BorderLayout.CENTER, which has a scale from -4 to 4 and 
    		 *starts out at the ICritter's happiness original happiness level. This JSlider represents 
    		 *value returned by ICritter.getHappiness(). 
    		 */
    		happinessSlider = new JSlider();
    		happinessSlider.setMinimum(-4);
    		happinessSlider.setMaximum(4);
    		happinessSlider.setValue((int) theCritter.getHappiness());
    		critterPanel.add(happinessSlider, BorderLayout.CENTER);
    		
    		/*Finally, this JPanel should contain a JList at BorderLayout.SOUTH, which lists the last 
    		 *eight ICritterMemoryEvents in the ICritter.
    		 */
    		memoriesListModel = new DefaultListModel();
    		memoriesList = new JList(memoriesListModel);
    		memoriesList.setPreferredSize(new Dimension(150, 200));
    		setupMemoriesList(theCritter.getMemories());
    		critterPanel.add(memoriesList, BorderLayout.SOUTH);
    		
    		//A JPanel with an ImageIcon containing the ICritter's image
    		ImageIcon picture = new ImageIcon("images/iCritter.jpg");
    		JLabel pictureLabel = new JLabel("", picture, JLabel.CENTER);
    		add(pictureLabel, BorderLayout.CENTER);
    		
    		//A JLabel with your name and the iCritter's name
    		JLabel names = new JLabel("iCritter: "+theOwner.getCritter().getName()+"   Owner: "+theOwner.getName());
    		add(names, BorderLayout.NORTH);
    		
    	}
    	
    	
    	//When the button is clicked, the ActionListener implementation calls Owner.buyFancyTreat().
    		private class buyFancyTreatListener implements ActionListener{
    			public void actionPerformed(ActionEvent e){
    				theOwner.buyFancyTreat("Fancy Treat");
    			}
    		}
    		
    		
    		//When the button is clicked, the ActionListener implementation calls Owner.buyCheapTreat().
    		private class buyCheapTreatListener implements ActionListener{
    			public void actionPerformed(ActionEvent e){
    				theOwner.buyCheapTreat("Cheap Treat");
    			}
    		}
    	
    	
    	/*When the button is clicked, the ActionListener implementation should see which 
    	 *treat is highlighted in the JList, remove that treat entry from the JList, and 
    	 *call Owner.giveTreat() on the corresponding treat in the Owner's treat list.
    	 */
    	private class giveTreatListener implements ActionListener{
    		public void actionPerformed(ActionEvent arg0) {
    			int index = treatsList.getSelectedIndex();
    			if(index == -1){
    				JOptionPane.showMessageDialog(null,new JLabel("Select a treat to be given."));
    			}
    			else{
    				Treat treatToGive = theOwner.listTreats().get(index);
    				theOwner.giveTreat(treatToGive);
    			}
    		}
    	}
    	
    	
    	/*The list should have an entry called “Fancy Treat” for each FancyTreat instance in 
    	 *the Owner's treat list, and it should have an entry called “Cheap Treat” for each 
    	 *CheapTreat instance in the Owner's treat list.
    	 */
    	private void setupTreatsList(java.util.List<Treat> theTreats){
    		treatsListModel.clear();
    		for(Treat i : theTreats){
    			if(i instanceof CheapTreat)
    				treatsListModel.addElement("Cheap Treat");
    			else if(i instanceof FancyTreat)
    				treatsListModel.addElement("Fancy Treat");
    		}
    		this.validate();
    	}
    
    	
    	/*Each entry should indicate the treat type (“Fancy Treat” or “Cheap Treat”) and the corresponding 
    	 *ICritterReaction.getMoodModifier() value (which should still be a positive integer). This list 
    	 *should be sorted from top-to-bottom where the most recent memory is at the bottom.
    	 */
    	private void setupMemoriesList(java.util.List<ICritterMemoryEvent> memories){
    		
    		int i;
    		if(memories.size() < 8)
    			i = 0;
    		else
    			i = memories.size() - 8;
    			java.util.List<ICritterMemoryEvent> lastEightMemories = memories.subList(i, memories.size());
    			memoriesListModel.clear();
    		for(ICritterMemoryEvent j : lastEightMemories){
    			String lineOutput = "";
    			if(j.getRememberedTreat() instanceof CheapTreat)
    				lineOutput += "Cheap Treat";
    			else if(j.getRememberedTreat() instanceof FancyTreat)
    				lineOutput += "Fancy Treat";
    			lineOutput += " (Reaction to "+j.getRememberedTreat()+" was: "+j.getRememberedReaction().getMoodModifier()+")";
    			memoriesListModel.addElement(lineOutput);
    		}
    	}
    
    
    	@Override
    	public void update(Observable arg0, Object arg1){
    		ICritterUpdate update = (ICritterUpdate)arg1;
    		if(update.getUpdateType() == ICritterUpdate.UPDATE_ICRITTER)
    			updateICritterComponents((ICritter)arg0);
    		else if(update.getUpdateType() == ICritterUpdate.UPDATE_OWNER)
    			updateOwnerComponents((Owner)arg0);
    	}
    	
    	
    	/*This method updates all of the iCritter-related components in the GUI to 
    	 *reflect the data within the given iCritter instance. It should be called 
    	 *from the update() method inherited from Observer, and even then it should 
    	 *only be called if the Object passed to update() is an instance of ICritterUpdate, 
    	 *and that instance's updateType member is equal to ICritterUpdate.UPDATE_iCRITTER.
    	 */
    	public void updateICritterComponents(ICritter theCritter){
    		happinessSlider.setValue((int)theCritter.getHappiness());
    		setupMemoriesList(theCritter.getMemories());
    	}
    	
    	
    	public void updateOwnerComponents(Owner theOwner){
    		setupTreatsList(theOwner.listTreats());
    		numCreditsLabel.setText(""+theOwner.getCredits());
    	}	
    }
    Here's the Owner.java
    Java Code:
    package model;
    import java.util.*;
    import view.ICritterUpdate;
    
    public class Owner extends Observable{
    	
    	private ICritter iCritter;
    	private List<Treat> treats;
    	private int credits;
    	private String name;
    	
    	
    	/*The Owner constructor should create an iCritter 
    	 * and initialize the credit count to 30 credits.
    	 */
    	public Owner(String ownerName, String critterName){
    		name = ownerName;
    		iCritter = new ICritter(critterName, this);
    		credits = 30;
    		treats = new LinkedList<Treat>();	
    	}
    	
    	
    	/*adjustCredits(Integer)
    	 *Adds the given Integer to the Owners's number of credits.
    	 */
    	private void adjustCredits(int amount){
    		credits += amount;
    		setChanged();
    		notifyObservers(new ICritterUpdate(ICritterUpdate.UPDATE_OWNER));
    	}
    	
    	
    	/*addTreat(Treat)
    	 *Add a Treat to the Owner's list of treats.
    	 */
    	public void addTreat(Treat aTreat){
    		treats.add(aTreat);
    		setChanged();
    		notifyObservers(new ICritterUpdate(ICritterUpdate.UPDATE_OWNER));
    	}
    	
    	
    	/*listTreats()
    	 *Return a list of the Ownder's treats
    	 *(this could be an empty list).
    	 */
    	public List<Treat> listTreats(){
    		return treats;
    	}
    	
    	
    	/*buyCheapTreat(String)
    	 *If the Owner can afford the CheapTreat then Create a
    	 *cheap treat with the given description string and
    	 *adjust the credit count.
    	 */
    	public CheapTreat buyCheapTreat(String descrip){
    		CheapTreat aTreat = new CheapTreat(descrip);
    		if(credits >= aTreat.getCost()){
    			adjustCredits(-aTreat.getCost());
    			addTreat(aTreat);
    			return aTreat;
    		}
    		else{
    			return null;
    		}
    	}
    	
    	
    	public FancyTreat buyFancyTreat(String descrip){
    		FancyTreat aTreat = new FancyTreat(descrip);
    		if(credits >= aTreat.getCost()){
    			adjustCredits(-aTreat.getCost());
    			addTreat(aTreat);
    			return aTreat;
    		}
    		else{
    			return null;
    		}
    	}
    	
    	
    	/*giveTreat(Treat)
    	 *If the Owner owns the treat, then give the treat
    	 *to the Critter and remove the Treat from the list treats.
    	 */
    	public ICritterReaction giveTreat(Treat aTreat){
    		if(treats.contains(aTreat)){
    			treats.remove(aTreat);
    			setChanged();
    			notifyObservers(new ICritterUpdate(ICritterUpdate.UPDATE_OWNER));
    			return iCritter.receiveTreat(aTreat);
    		}
    		else
    			return null;
    	}
    	
    	
    	//getCritter()
    	public ICritter getCritter(){
    		return iCritter;
    	}
    	
    	
    	//getName()
    	public String getName(){
    		return name;
    	}
    	
    	
    	//getCredits
    	public Integer getCredits(){
    		return credits;
    	}	
    }
    And here's the updated stack trace:
    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at view.ICritterView.updateOwnerComponents(ICritterView.java:222)
    	at view.ICritterView.update(ICritterView.java:204)
    	at java.util.Observable.notifyObservers(Observable.java:142)
    	at model.Owner.adjustCredits(Owner.java:30)
    	at model.Owner.buyCheapTreat(Owner.java:61)
    	at view.ICritterView$buyCheapTreatListener.actionPerformed(ICritterView.java:134)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    	at java.awt.Component.processMouseEvent(Component.java:6375)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    	at java.awt.Component.processEvent(Component.java:6140)
    	at java.awt.Container.processEvent(Container.java:2083)
    	at java.awt.Component.dispatchEventImpl(Component.java:4737)
    	at java.awt.Container.dispatchEventImpl(Container.java:2141)
    	at java.awt.Component.dispatchEvent(Component.java:4565)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4619)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4280)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4210)
    	at java.awt.Container.dispatchEventImpl(Container.java:2127)
    	at java.awt.Window.dispatchEventImpl(Window.java:2482)
    	at java.awt.Component.dispatchEvent(Component.java:4565)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:684)
    	at java.awt.EventQueue.access$000(EventQueue.java:85)
    	at java.awt.EventQueue$1.run(EventQueue.java:643)
    	at java.awt.EventQueue$1.run(EventQueue.java:641)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    	at java.awt.EventQueue$2.run(EventQueue.java:657)
    	at java.awt.EventQueue$2.run(EventQueue.java:655)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:654)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    Line 222 in ICritterView is:
    Java Code:
    numCreditsLabel.setText(""+theOwner.getCredits());
    I know where the problem is, I just don't know how to fix it. Any suggestions?

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: NullPointerException Help!

    Quote Originally Posted by amazin112 View Post
    Line 222 in ICritterView is:
    Java Code:
    numCreditsLabel.setText(""+theOwner.getCredits());
    I know where the problem is, I just don't know how to fix it. Any suggestions?
    Yes, make sure that you have initialized both the numCreditsLabel and the theOwner variables. If not, then do this before trying to use them.

  19. #19
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: NullPointerException Help!

    Also be wary about "variable shadowing" as this is the source of your current problem.

    For example, please see my answers to similar questions here and here.
    Last edited by Fubarable; 04-30-2012 at 03:38 AM.

  20. #20
    amazin112 is offline Member
    Join Date
    Apr 2012
    Posts
    10
    Rep Power
    0

    Default Re: NullPointerException Help!

    Quote Originally Posted by Fubarable View Post
    Also be wary about "variable shadowing" as this is the source of your current problem.

    For example, please see my answers to similar questions here and here.
    Holy crap... you are absolutely right. One word 'JLabel' was keeping the whole program from working. I found the "variable shadowing" problem that you are talking about and have fixed it. Thank you very much for your help!
    Fubarable likes this.

Page 1 of 2 12 LastLast

Similar Threads

  1. NullPointerException
    By jayragz in forum NetBeans
    Replies: 5
    Last Post: 05-12-2011, 06:19 PM
  2. Nullpointerexception?
    By Pulgo in forum New To Java
    Replies: 5
    Last Post: 01-05-2011, 05:05 PM
  3. NullPointerException
    By s0meb0dy in forum New To Java
    Replies: 3
    Last Post: 10-09-2010, 09:12 PM
  4. NullPointerException I NEED HELP
    By mayhewj7 in forum New To Java
    Replies: 2
    Last Post: 02-13-2009, 09:03 AM
  5. I get a NullPointerException and don't know why
    By hendrix79 in forum New To Java
    Replies: 9
    Last Post: 12-14-2008, 07:18 AM

Tags for this Thread

Posting Permissions

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