Null Pointer Exception

gDrawString problem

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

30 Replies - 2128 Views - Last Post: 16 September 2008 - 07:31 PM Rate Topic: -----

#1 skin__  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 20-May 08

Null Pointer Exception

Post icon  Posted 10 September 2008 - 01:04 AM

Hey guys,
I'm just finishing up a project where we have to create a program that simulates the plumbing in the house. I'm almost finished but not I'm running across a Null Pointer Exception.

Here's what I have:

-----------------------------

This post has been edited by skin__: 10 September 2008 - 05:04 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Null Pointer Exception

#2 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Null Pointer Exception

Posted 10 September 2008 - 02:18 AM

Please post the error as well (stack trace included, thank you), because it contains the line where the exception occurs, so we can have a look at it even without compiling and running your program.
Was This Post Helpful? 0
  • +
  • -

#3 skin__  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 20-May 08

Re: Null Pointer Exception

Posted 10 September 2008 - 03:12 AM

Okay, thanks. I'm getting Null Pointer Exception error on this part of code:

	public void calculateFlow()
	{
		sewerIn.calculateFlow(0);  
	}


Which is in the second part of the code I posted.

This post has been edited by skin__: 10 September 2008 - 03:13 AM

Was This Post Helpful? 0
  • +
  • -

#4 skin__  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 20-May 08

Re: Null Pointer Exception

Posted 10 September 2008 - 06:33 PM

I fixed up my last problem, so now the GUI is working fine but the outputs aren't right. Here's my code:

public class Lab5
{
	public static void main(String[] args)
	{
		Lab5Frame frame = new Lab5Frame();
		frame.setVisible(true);
	}
}


import java.awt.*;
import java.awt.event.*;


public class Lab5Frame extends Frame implements ActionListener
{
	protected final int LARGE=300;							// maximum capacity
	protected final int MEDIUM=25;							// medium capacity
	protected final int SMALL=8;							// minimum capacity
	
	protected TextField inFlowTF1,inFlowTF2,inFlowTF3;		// TextFields for available liquid
	protected TextField outFlowTF1,outFlowTF2,outFlowTF3;	// Textfields for out flow
	protected Button plumbB1,plumbB2,plumbB3,resetB;		// Buttons to control fittings or to reset
	protected PlumbingProject cleanWaterIn,cleanWaterOut;	// source and trap for clean water
	protected PlumbingProject sewerIn,sewerOut;				// source and trap for sewage
	protected PlumbingItem basin,shower,toilet;				// fittings
	
	public Lab5Frame()
	{
		MenuBar menuBar = new MenuBar();
		Menu menuFile = new Menu();
		MenuItem menuFileExit = new MenuItem();
		
		menuFile.setLabel("File");
		menuFileExit.setLabel("Exit");
		
		// Add action listener.for the menu button
		menuFileExit.addActionListener
		(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Lab5Frame.this.windowClosed();
				}
			}
		); 
		menuFile.add(menuFileExit);
		menuBar.add(menuFile);
		
		setTitle("Lab5");
		setMenuBar(menuBar);
		setSize(new Dimension(350, 350));
		
		// Add window listener.
		this.addWindowListener
		(
			new WindowAdapter() {
				public void windowClosing(WindowEvent e) {
					Lab5Frame.this.windowClosed();
				}
			}
		);  
		
		setUpGUI();
		
		resetPlumbing();
 	}
	
	
	protected void windowClosed()
	{		
		// Exit application.
		System.exit(0);
	}
	
	
	public void setUpGUI()
	{
	   	setLayout(new FlowLayout());
		inFlowTF1=new TextField(10);
		inFlowTF1.setEditable(false);
		inFlowTF1.setVisible(true);
		inFlowTF2=new TextField(10);
		inFlowTF2.setEditable(false);
		inFlowTF2.setVisible(true);
		inFlowTF3=new TextField(10);
		inFlowTF3.setEditable(false);
		inFlowTF3.setVisible(true);
		outFlowTF1=new TextField(10);
		outFlowTF1.setEditable(false);
		outFlowTF1.setVisible(true);
		outFlowTF2=new TextField(10);
		outFlowTF2.setEditable(false);
		outFlowTF2.setVisible(true);
		outFlowTF3=new TextField(10);
		outFlowTF3.setEditable(false);
		outFlowTF3.setVisible(true);
		
		plumbB1=new Button("Turn on basin tap");
		plumbB1.addActionListener(this);
		plumbB1.setVisible(true);
		add(inFlowTF1);
		add(plumbB1);
		add(outFlowTF1);
		
		plumbB2=new Button("Turn on shower");
		plumbB2.addActionListener(this);
		plumbB2.setVisible(true);
		add(inFlowTF2);
		add(plumbB2);
		add(outFlowTF2);
		
		plumbB3=new Button("Flush toilet");
		plumbB3.addActionListener(this);
		plumbB3.setVisible(true);
		add(inFlowTF3);
		add(plumbB3);
		add(outFlowTF3);
		
		resetB=new Button("Reset");
		resetB.addActionListener(this);
		resetB.setVisible(true);
		add(resetB);
	}
	
	
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() == plumbB1)
		{
			flushToilet();
		}
		if (e.getSource() == plumbB2)
		{
			turnOnShower();
		}
		if (e.getSource() == plumbB3)
		{
			flushToilet();
		}
		if (e.getSource() == resetB)
		{
			resetPlumbing();
		}
		repaint();
	}
	
	
	public void resetPlumbing()
	{
		setUpPlumbing();
		calculateFlow();
	}
		
	public void setUpPlumbing()
	{
		PlumbingProject c1,c2;
		PlumbingProject b,s,t;
		PlumbingItem pipeSegment;
		PlumbingItem teeSegment;
		
		basin=new PlumbingItem("basin");
		basin.setInFlowCapacity1(SMALL);
		basin.setOutFlowCapacity1(SMALL);
		b=new PlumbingProject(basin);
		shower=new PlumbingItem("shower");
		shower.setInFlowCapacity1(MEDIUM);
		shower.setOutFlowCapacity1(MEDIUM);
		s=new PlumbingProject(shower);
		toilet=new PlumbingItem("toilet");
		toilet.setInFlowCapacity1(SMALL);
		toilet.setOutFlowCapacity1(MEDIUM);
		t=new PlumbingProject(toilet);
		
		pipeSegment=new PlumbingItem("pipe1",false,false,LARGE);
		teeSegment=new PlumbingItem("tee1",true,false,LARGE);   	
		cleanWaterIn=new PlumbingProject(pipeSegment);
		cleanWaterIn.connectTo(teeSegment,1,1);
		c1=cleanWaterIn.getConnection(1);
		pipeSegment=new PlumbingItem("pipe5",false,false,LARGE);
		c1.connectTo(pipeSegment,2,1);
		c2=c1.getConnection(2);
		c2.connectTo(b,1,1);
 
	 	pipeSegment=new PlumbingItem("pipe2",false,false,LARGE);
		teeSegment=new PlumbingItem("tee2",true,false,LARGE);
		c1.connectTo(pipeSegment,1,1);
		c1=c1.getConnection(1);
		c1.connectTo(teeSegment,1,1);
		c1=c1.getConnection(1);
		pipeSegment=new PlumbingItem("pipe6",false,false,LARGE);
		c1.connectTo(pipeSegment,2,1);
		c2=c1.getConnection(2);
		c2.connectTo(s,1,1);
		
	 	pipeSegment=new PlumbingItem("pipe3",false,false,LARGE);
		teeSegment=new PlumbingItem("tee3",true,false,LARGE);
		c1.connectTo(pipeSegment,1,1);
		c1=c1.getConnection(1);
		c1.connectTo(teeSegment,1,1);
		c1=c1.getConnection(1);
		pipeSegment=new PlumbingItem("pipe7",false,false,LARGE);
		c1.connectTo(pipeSegment,2,1);
		c2=c1.getConnection(2);
		c2.connectTo(t,1,1);
		
	 	pipeSegment=new PlumbingItem("pipe4",false,false,LARGE);
	 	cleanWaterOut=new PlumbingProject(pipeSegment);
		c1.connectTo(pipeSegment,1,1);

		pipeSegment=new PlumbingItem("pipe11",false,false,LARGE);
		teeSegment=new PlumbingItem("tee4",true,true,LARGE);   	
		sewerIn=new PlumbingProject(pipeSegment);
		sewerIn.connectTo(teeSegment,1,1);
		c1=sewerIn.getConnection(1);
		pipeSegment=new PlumbingItem("pipe8",false,false,LARGE);
		c2=new PlumbingProject(pipeSegment);
		b.connectTo(c2,1,1);
		c2.connectTo(c1,1,2);
 
	 	pipeSegment=new PlumbingItem("pipe12",false,false,LARGE);
		teeSegment=new PlumbingItem("tee5",true,true,LARGE);
		c1.connectTo(pipeSegment,1,1);
		c1=c1.getConnection(1);
		c1.connectTo(teeSegment,1,1);
		c1=c1.getConnection(1);
		pipeSegment=new PlumbingItem("pipe9",false,false,LARGE);
		c2=new PlumbingProject(pipeSegment);
		s.connectTo(c2,1,1);
		c2.connectTo(c1,1,2);
		
	 	pipeSegment=new PlumbingItem("pipe13",false,false,LARGE);
		teeSegment=new PlumbingItem("tee6",true,true,LARGE);
		c1.connectTo(pipeSegment,1,1);
		c1=c1.getConnection(1);
		c1.connectTo(teeSegment,1,1);
		c1=c1.getConnection(1);
		pipeSegment=new PlumbingItem("pipe10",false,false,LARGE);
		c2=new PlumbingProject(pipeSegment);
		t.connectTo(c2,1,1);
		c2.connectTo(c1,1,2);
		
	 	pipeSegment=new PlumbingItem("pipe14",false,false,SMALL);
	 	sewerOut=new PlumbingProject(pipeSegment);
		c1.connectTo(pipeSegment,1,1);
	}
	
	
	public void calculateFlow()
	{
		sewerIn.calculateFlow(0);   
	}
	
	
	public void turnOnBasin()
	{
		basin.setThroughFlow(true);
	 	calculateFlow();
   	}
	
	
	public void turnOnShower()
	{
	 	shower.setThroughFlow(true);
	 	calculateFlow();
   	}
	
	
	public void flushToilet()
	{
	 	toilet.setThroughFlow(true);
	 	calculateFlow();
   	}
	
	
	public void paint(Graphics g)
	{
		inFlowTF1.setText("Available "+Math.min(basin.getInFlowRate1(),basin.getInFlowCapacity1()));
		inFlowTF2.setText("Available "+Math.min(shower.getInFlowRate1(),shower.getInFlowCapacity1()));
		inFlowTF3.setText("Available "+Math.min(toilet.getInFlowRate1(),toilet.getInFlowCapacity1()));
		outFlowTF1.setText("Actual "+basin.getOutFlowRate1());
		outFlowTF2.setText("Actual "+shower.getOutFlowRate1());
		outFlowTF3.setText("Actual "+toilet.getOutFlowRate1());
		
		g.drawString("Total clean water entering system:" + cleanWaterIn.getItem().getOutFlowRate1(),20,200);
		g.drawString("Total clean water leaving system:" + cleanWaterOut.getItem().getOutFlowRate1(),20,220);
		g.drawString("Total sewage entering system:" + sewerIn.getItem().getOutFlowRate1(),20,240);
		g.drawString("Total sewage leaving system:" + sewerOut.getItem().getOutFlowRate1(),20,260);
		
		if (basin.getOutFlowRate1()>sewerOut.getItem().getOutFlowRate1())
		{
			g.drawString("Basin has overflowed!!!",20,300);
		}
		if (shower.getOutFlowRate1()>sewerOut.getItem().getOutFlowRate1())
		{
			g.drawString("Shower has overflowed!!!",20,320);
		}
		if (toilet.getOutFlowRate1()>sewerOut.getItem().getOutFlowRate1())
		{
			g.drawString("Toilet has overflowed!!!",20,340);
		}
	}
}


public class PlumbingProject
{
	protected Node firstItem;		// the first node in the plumbing network


	public PlumbingProject()
	{
		firstItem=null;
	}


	public PlumbingProject(PlumbingItem o)
	{
		firstItem=new Node(o);
	}
	
	
	public boolean isEmpty()
	{
		return (firstItem == null);
	}


	public PlumbingItem getItem() throws RuntimeException
	{
		if (isEmpty())
		{
			throw new RuntimeException("No plumbing item found?");
		}
		else
		{
			return firstItem.getData();
		}
	}


	public PlumbingProject getConnection(int n) throws RuntimeException
	{
		PlumbingProject p=new PlumbingProject();
		
		if (isEmpty())
		{
			throw new RuntimeException("No plumbing item found?");
		}
		else
		{
			if (n==1)
			{
				p.firstItem=firstItem.getOutlet1();
			}
			else
			{
				p.firstItem=firstItem.getOutlet2();
			}
		}
		
		return p;
	}


	public void connectTo(PlumbingItem o,int out,int in)
	{
		Node n;
		
		n=new Node(o);
		
		if (out==1)
		{
			firstItem.setOutlet1(n);
		}
		else
		{
			firstItem.setOutlet2(n);
		}
		
		if (in==1)
		{
			n.setInlet1(firstItem);
		}
		else
		{
			n.setInlet2(firstItem);
		}
	}
	
	
	public void connectTo(PlumbingProject o,int out,int in)
	{
		if (out==1)
		{
			firstItem.setOutlet1(o.firstItem);
		}
		else
		{
			firstItem.setOutlet2(o.firstItem);
		}
		
		if (in==1)
		{
			o.firstItem.setInlet1(firstItem);
		}
		else
		{
			o.firstItem.setInlet2(firstItem);
		}
	}
	
	
	public void calculateFlow(int rate)
	{
		PlumbingProject p,c;
		PlumbingItem i;
		int oldRate=0;
		int newRate=0;
		boolean done;
		
		p=null;
		c=this;
		oldRate=rate;
		done=(c.isEmpty());
		while (! done)
		{
			i=c.getItem();
			if (! i.isTee())
			{
				i.setInFlowRate1(oldRate);
				newRate=i.calculateFlow();
				
				oldRate=newRate;
				p=c;
				c=c.getConnection(1);
			}
			else
			{
				if (i.isReverseTee())
				{
					if (p.firstItem==c.firstItem.getInlet1())
					{
						i.setInFlowRate1(oldRate);
						newRate=i.calculateFlow();
						
						oldRate=newRate;
						p=c;
						c=c.getConnection(1);
					}
					else
					{
						i.setInFlowRate2(oldRate);
						done=true;
					}
				}
				else
				{
					i.setInFlowRate1(oldRate);
					newRate=i.calculateFlow();
					newRate=i.getOutFlowRate2();

					oldRate=newRate;
					p=c;
					c=c.getConnection(2);
					c.calculateFlow(oldRate);
					
					c=p.getConnection(1);
				}
			}
			done=done||(c.isEmpty());
		}
	}
	
	public String toString()
	{
		Node c;
		PlumbingProject p;
		String s="";
		String s2;
		
		if (isEmpty())
		{
			return "";
		}
		else
		{
			c=firstItem;
			while (c != null)
			{
				s2=c.getData().toString();
				s=s.concat(s2 + " ");
				if (c.getData().isTee())
				{
					s+="(";
					p=new PlumbingProject();
					p.firstItem=c.getOutlet2();
					s+=p.toString();
					s+=") ";
				}
				c=c.getOutlet1();
			}
			return s;
		}
		
	}
}


public class PlumbingItem
{
	protected boolean tee;				// is this pipe/fitting a tee-junction?
	protected boolean reverse;			// if a tee-junction, is it a 2-in and 1-out?
	protected boolean throughFlow;		// if a fitting, is water flowing through it, i.e. is it open or closed?
	protected String name;				// what is the name of the pipe/fitting
	
	protected int inFlowRate1;			// what water volume is actually flowing in through inlet 1?
	protected int inFlowRate2;			// what water volume is actually flowing in through inlet 2 (if it exists)?
	protected int outFlowRate1;			// what water volume is actually flowing out through outlet 1?
	protected int outFlowRate2;			// what water volume is actually flowing out through outlet 2 (if it exists)?
	protected int inFlowCapacity1;		// what is the maximum water volume possible for inlet 1?
	protected int inFlowCapacity2;		// what is the maximum water volume possible for inlet 2 (if it exists)?
	protected int outFlowCapacity1;		// what is the maximum water volume possible for outlet 1?
	protected int outFlowCapacity2;		// what is the maximum water volume possible for outlet 2 (if it exists)?
	
	
	public PlumbingItem(String s)
	{
		tee=false;
		reverse=false;
		if ((s.indexOf("tee") != -1) || (s.indexOf("pipe") != -1))
		{
			throughFlow=true;
		}
		else
		{
			throughFlow=false;
		}
		name=s;
		inFlowRate1=0;
		inFlowRate2=0;
		outFlowRate1=0;
		outFlowRate2=0;
		inFlowCapacity1=0;
		inFlowCapacity2=0;
		outFlowCapacity1=0;
		outFlowCapacity2=0;
	}
	
	
	public PlumbingItem(String s,boolean t,boolean r,int capacity)
	{
		tee=t;
		reverse=r;
		if ((s.indexOf("tee") != -1) || (s.indexOf("pipe") != -1))
		{
			throughFlow=true;
		}
		else
		{
			throughFlow=false;
		}
		name=s;
		inFlowRate1=0;
		inFlowRate2=0;
		outFlowRate1=0;
		outFlowRate2=0;
		if (tee)
		{
			if (reverse)
			{
				inFlowCapacity1=capacity;
				inFlowCapacity2=capacity;
				outFlowCapacity1=capacity;
				outFlowCapacity2=0;
			}
			else
			{
				inFlowCapacity1=capacity;
				inFlowCapacity2=0;
				outFlowCapacity1=capacity;
				outFlowCapacity2=capacity;
			}
		}
		else
		{
			inFlowCapacity1=capacity;
			inFlowCapacity2=0;
			outFlowCapacity1=capacity;
			outFlowCapacity2=0;
		}
	}
	
	
	public void setThroughFlow(boolean status)
	{
		throughFlow=status;
	}
	
	
	public void setInFlowRate1(int flow)
	{
		inFlowRate1=flow;
	}
	
	
	public void setInFlowRate2(int flow)
	{
		inFlowRate2=flow;
	}
	
	
	public void setOutFlowRate1(int flow)
	{
		outFlowRate1=flow;
	}
	
	
	public void setOutFlowRate2(int flow)
	{
		outFlowRate2=flow;
	}
	
	
	public void setInFlowCapacity1(int capacity)
	{
		inFlowCapacity1=capacity;
	}
	
	
	public void setInFlowCapacity2(int capacity)
	{
		inFlowCapacity2=capacity;
		if (capacity !=0)
		{
			tee=true;
			reverse=true;
		}
		else
		{
			tee=false;
		}
	}
	
	
	public void setOutFlowCapacity1(int capacity)
	{
		outFlowCapacity1=capacity;
	}
	
	
	public void setOutFlowCapacity2(int capacity)
	{
		outFlowCapacity2=capacity;
		if (capacity !=0)
		{
			tee=true;
		}
		else
		{
			tee=false;
		}
	}
	
	
	public boolean isTee()
	{
		return tee;
	}
	
	
	public boolean isReverseTee()
	{
		return reverse;
	}
	
	
	public boolean isOpen()
	{
		return throughFlow;
	}
	
	
	public String getName()
	{
		return name;
	}
	
	
	public int getInFlowRate1()
	{
		return inFlowRate1;
	}
	
	
	public int getInFlowRate2()
	{
		return inFlowRate2;
	}
	
	
	public int getOutFlowRate1()
	{
		return outFlowRate1;
	}
	
	
	public int getOutFlowRate2()
	{
		return outFlowRate2;
	}
	
	
	public int getInFlowCapacity1()
	{
		return inFlowCapacity1;
	}
	
	
	public int getInFlowCapacity2()
	{
		return inFlowCapacity2;
	}
	
	
	public int getOutFlowCapacity1()
	{
		return outFlowCapacity1;
	}
	
	
	public int getOutFlowCapacity2()
	{
		return outFlowCapacity2;
	}
	
	
	public int calculateFlow()
	{
		int flow;
		int flow1=0;
		int flow2=0;

		if (isOpen())
		{
			if (!isTee())
			{
				flow1=Math.min(getInFlowRate1(),getOutFlowCapacity1());
			}
			else
			{
				if (isReverseTee())
				{
					flow1=Math.min(getInFlowRate1()+getInFlowRate2(),getOutFlowCapacity1());
				}
				else
				{
					if (getOutFlowCapacity1()+getOutFlowCapacity2() < getInFlowRate1())
					{
						flow1=getOutFlowCapacity1();
						flow2=getOutFlowCapacity2();
					}
					else
					{
						flow1=getInFlowRate1()/2;
						flow2=getInFlowRate1()-flow1;
						if (flow1>getOutFlowCapacity1())
						{
							flow1=getOutFlowCapacity1();
							flow2=getInFlowRate1()-flow1;
						}
						if (flow2>getOutFlowCapacity2())
						{
							flow2=getOutFlowCapacity2();
							flow1=getInFlowRate1()-flow2;
						}
					}
				}
			}
		}
		
		setOutFlowRate1(flow1);
		setOutFlowRate2(flow2);
		flow=flow1+flow2;

		return flow;
	}
	
	public String toString()
	{
		return getName() + " [" + inFlowCapacity1 + "," + inFlowCapacity2 + ";" + outFlowCapacity1 
					+ "," + outFlowCapacity2 + "] @ " + outFlowRate1 + " + " + outFlowRate2;		

	}
}


[code]public class Node
{
protected PlumbingItem data; // the item value stored in the node
protected Node inlet1; // a reference to the previous pipe segment/plumbing fixture (if any)
protected Node inlet2; // a reference to the previous pipe segment/plumbing fixture (if any)
protected Node outlet1; // a reference to the next pipe segment/plumbing fixture (if any)
protected Node outlet2; // a reference to the next pipe segment/plumbing fixture (if any)


public Node(PlumbingItem o)
{
data=o;
inlet1=null;
inlet2=null;
outlet1=null;
outlet2=null;
}


public void setData(PlumbingItem o)
{
data=o;
}


public void setInlet1(Node n)
{
inlet1=n;
}


public void setInlet2(Node n)
{
inlet2=n;
}


public void setOutlet1(Node n)
{
outlet1=n;
}


public void setOutlet2(Node n)
&
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Null Pointer Exception

Posted 10 September 2008 - 07:11 PM

View Postskin__, on 10 Sep, 2008 - 06:33 PM, said:

I fixed up my last problem, so now the GUI is working fine but the outputs aren't right. Here's my code:


Your posting of your class Node is not complete.

What is exactly your problem ? Can't you isolate it ?
Background color not correct ? Textfield does not react ?

We won't read hundreds of lines of code trying to figure out what it does that you don't want it to do.

Try to isolate the problem so you can post a lot less lines of code.

This post has been edited by pbl: 10 September 2008 - 07:12 PM

Was This Post Helpful? 0
  • +
  • -

#6 skin__  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 20-May 08

Re: Null Pointer Exception

Posted 10 September 2008 - 07:26 PM

The problem is that when I compile/run the program the text fields are only displaying 0, instead of the values I have assigned to them in the second block of code.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Null Pointer Exception

Posted 10 September 2008 - 07:58 PM

View Postskin__, on 10 Sep, 2008 - 07:26 PM, said:

The problem is that when I compile/run the program the text fields are only displaying 0, instead of the values I have assigned to them in the second block of code.

OK I completed your Node class
Cut & paste your code
Now, I tried to click on the JButton nothing happens... what is it supposed to do ?
Was This Post Helpful? 0
  • +
  • -

#8 skin__  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 20-May 08

Re: Null Pointer Exception

Posted 10 September 2008 - 08:08 PM

Ah sorry about that node class, here's the finished version:
public class Node
{
	protected PlumbingItem data;			// the item value stored in the node
	protected Node inlet1;					// a reference to the previous pipe segment/plumbing fixture (if any)
	protected Node inlet2;					// a reference to the previous pipe segment/plumbing fixture (if any)
	protected Node outlet1;					// a reference to the next pipe segment/plumbing fixture (if any)
	protected Node outlet2;					// a reference to the next pipe segment/plumbing fixture (if any)
  
  
	public Node(PlumbingItem o)
	{
		data=o;
		inlet1=null;
		inlet2=null;
		outlet1=null;
		outlet2=null;
	}
	
	
	public void setData(PlumbingItem o)
	{
		data=o;
	}
	
	
	public void setInlet1(Node n)
	{
		inlet1=n;
	}
	
	
	public void setInlet2(Node n)
	{
		inlet2=n;
	}
	
	
	public void setOutlet1(Node n)
	{
		outlet1=n;
	}
	
	
	public void setOutlet2(Node n)
	{
		outlet2=n;
	}
	
	
	public PlumbingItem getData()
	{
		return data;
	}
	
	
	public Node getInlet1()
	{
		return inlet1;
	}


	public Node getInlet2()
	{
		return inlet2;
	}
	
	
	public Node getOutlet1()
	{
		return outlet1;
	}


	public Node getOutlet2()
	{
		return outlet2;
	}
}


When you click the JButtons it's meant to update the g.drawStrings down the bottom of the GUI. Thankyou so much for your help, pbl.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Null Pointer Exception

Posted 10 September 2008 - 08:29 PM

View Postskin__, on 10 Sep, 2008 - 08:08 PM, said:

Ah sorry about that node class, here's the finished version:


Yes I figured it out

View Postskin__, on 10 Sep, 2008 - 08:08 PM, said:

When you click the JButtons it's meant to update the g.drawStrings down the bottom of the GUI. Thankyou so much for your help, pbl.


OK flush the toilet
Got into ActionListener which calls calculateFlow of PlumbingProject
the while is executed 7 times and then execute

Saw nothing to update the GUI there
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Null Pointer Exception

Posted 10 September 2008 - 08:36 PM

One thing for sure... your paint() method should call its super unless you plan to update your JTextField yourself

   public void paint(Graphics g)
	{
		inFlowTF1.setText("Available "+Math.min(basin.getInFlowRate1(),basin.getInFlowCapacity1()));
		inFlowTF2.setText("Available "+Math.min(shower.getInFlowRate1(),shower.getInFlowCapacity1()));
		inFlowTF3.setText("Available "+Math.min(toilet.getInFlowRate1(),toilet.getInFlowCapacity1()));
		outFlowTF1.setText("Actual "+basin.getOutFlowRate1());
		outFlowTF2.setText("Actual "+shower.getOutFlowRate1());
		outFlowTF3.setText("Actual "+toilet.getOutFlowRate1());
		super.paint(g);
...



BTW why using JTextField if they are not editable ? Use JLabel instead... but not related to your problem
Was This Post Helpful? 0
  • +
  • -

#11 skin__  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 20-May 08

Re: Null Pointer Exception

Posted 10 September 2008 - 10:10 PM

Thanks for the reply, pbl. I have changed a few things with my Lab5Frame class, but the buttons still aren't working and the values in the g.drawStrings() still aren't properly displayed.
Slightly updated code:

import java.awt.*;
import java.awt.event.*;


public class Lab5Frame extends Frame implements ActionListener
{
	protected final int LARGE=300;							// maximum capacity
	protected final int MEDIUM=25;							// medium capacity
	protected final int SMALL=8;							// minimum capacity
	
	protected TextField inFlowTF1,inFlowTF2,inFlowTF3;		// TextFields for available liquid
	protected TextField outFlowTF1,outFlowTF2,outFlowTF3;	// Textfields for out flow
	protected Button plumbB1,plumbB2,plumbB3,resetB;		// Buttons to control fittings or to reset
	protected PlumbingProject cleanWaterIn,cleanWaterOut;	// source and trap for clean water
	protected PlumbingProject sewerIn,sewerOut;				// source and trap for sewage
	protected PlumbingItem basin,shower,toilet;				// fittings
	
	public Lab5Frame()
	{
		MenuBar menuBar = new MenuBar();
		Menu menuFile = new Menu();
		MenuItem menuFileExit = new MenuItem();
		
		menuFile.setLabel("File");
		menuFileExit.setLabel("Exit");
		
		// Add action listener.for the menu button
		menuFileExit.addActionListener
		(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Lab5Frame.this.windowClosed();
				}
			}
		); 
		menuFile.add(menuFileExit);
		menuBar.add(menuFile);
		
		setTitle("Lab5");
		setMenuBar(menuBar);
		setSize(new Dimension(350, 350));
		
		// Add window listener.
		this.addWindowListener
		(
			new WindowAdapter() {
				public void windowClosing(WindowEvent e) {
					Lab5Frame.this.windowClosed();
				}
			}
		);  
		
		setUpGUI();
		
		resetPlumbing();
 	}
	
	
	protected void windowClosed()
	{		
		System.exit(0);
	}
	
	
	public void setUpGUI()
	{
	   	setLayout(new FlowLayout());
		inFlowTF1=new TextField(10);
		inFlowTF1.setEditable(false);
		inFlowTF1.setVisible(true);
		inFlowTF2=new TextField(10);
		inFlowTF2.setEditable(false);
		inFlowTF2.setVisible(true);
		inFlowTF3=new TextField(10);
		inFlowTF3.setEditable(false);
		inFlowTF3.setVisible(true);
		outFlowTF1=new TextField(10);
		outFlowTF1.setEditable(false);
		outFlowTF1.setVisible(true);
		outFlowTF2=new TextField(10);
		outFlowTF2.setEditable(false);
		outFlowTF2.setVisible(true);
		outFlowTF3=new TextField(10);
		outFlowTF3.setEditable(false);
		outFlowTF3.setVisible(true);
		
		plumbB1=new Button("Turn on basin tap");
		plumbB1.addActionListener(this);
		plumbB1.setVisible(true);
		add(inFlowTF1);
		add(plumbB1);
		add(outFlowTF1);
		
		plumbB2=new Button("Turn on shower");
		plumbB2.addActionListener(this);
		plumbB2.setVisible(true);
		add(inFlowTF2);
		add(plumbB2);
		add(outFlowTF2);
		
		plumbB3=new Button("Flush toilet");
		plumbB3.addActionListener(this);
		plumbB3.setVisible(true);
		add(inFlowTF3);
		add(plumbB3);
		add(outFlowTF3);
		
		resetB=new Button("Reset");
		resetB.addActionListener(this);
		resetB.setVisible(true);
		add(resetB);
	}
	
	
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() == plumbB1)
		{
			turnOnBasin();
		}
		if (e.getSource() == plumbB2)
		{
			turnOnShower();
		}
		if (e.getSource() == plumbB3)
		{
			flushToilet();
		}
		if (e.getSource() == resetB)
		{
			resetPlumbing();
		}
		repaint();
	}
	
	
	public void resetPlumbing()
	{
		setUpPlumbing();
		calculateFlow();
	}
		
	public void setUpPlumbing()
	{
		PlumbingProject c1,c2;
		PlumbingProject b,s,t;
		PlumbingItem pipeSegment;
		PlumbingItem teeSegment;
		
		basin=new PlumbingItem("basin");
		basin.setInFlowCapacity1(SMALL);
		basin.setOutFlowCapacity1(SMALL);
		b=new PlumbingProject(basin);
		shower=new PlumbingItem("shower");
		shower.setInFlowCapacity1(MEDIUM);
		shower.setOutFlowCapacity1(MEDIUM);
		s=new PlumbingProject(shower);
		toilet=new PlumbingItem("toilet");
		toilet.setInFlowCapacity1(SMALL);
		toilet.setOutFlowCapacity1(MEDIUM);
		t=new PlumbingProject(toilet);
		
		pipeSegment=new PlumbingItem("pipe1",false,false,LARGE);
		teeSegment=new PlumbingItem("tee1",true,false,LARGE);   	
		cleanWaterIn=new PlumbingProject(pipeSegment);
		cleanWaterIn.connectTo(teeSegment,1,1);
		c1=cleanWaterIn.getConnection(1);
		pipeSegment=new PlumbingItem("pipe5",false,false,LARGE);
		c1.connectTo(pipeSegment,2,1);
		c2=c1.getConnection(2);
		c2.connectTo(b,1,1);
 
	 	pipeSegment=new PlumbingItem("pipe2",false,false,LARGE);
		teeSegment=new PlumbingItem("tee2",true,false,LARGE);
		c1.connectTo(pipeSegment,1,1);
		c1=c1.getConnection(1);
		c1.connectTo(teeSegment,1,1);
		c1=c1.getConnection(1);
		pipeSegment=new PlumbingItem("pipe6",false,false,LARGE);
		c1.connectTo(pipeSegment,2,1);
		c2=c1.getConnection(2);
		c2.connectTo(s,1,1);
		
	 	pipeSegment=new PlumbingItem("pipe3",false,false,LARGE);
		teeSegment=new PlumbingItem("tee3",true,false,LARGE);
		c1.connectTo(pipeSegment,1,1);
		c1=c1.getConnection(1);
		c1.connectTo(teeSegment,1,1);
		c1=c1.getConnection(1);
		pipeSegment=new PlumbingItem("pipe7",false,false,LARGE);
		c1.connectTo(pipeSegment,2,1);
		c2=c1.getConnection(2);
		c2.connectTo(t,1,1);
		
	 	pipeSegment=new PlumbingItem("pipe4",false,false,LARGE);
	 	cleanWaterOut=new PlumbingProject(pipeSegment);
		c1.connectTo(pipeSegment,1,1);

		pipeSegment=new PlumbingItem("pipe11",false,false,LARGE);
		teeSegment=new PlumbingItem("tee4",true,true,LARGE);   	
		sewerIn=new PlumbingProject(pipeSegment);
		sewerIn.connectTo(teeSegment,1,1);
		c1=sewerIn.getConnection(1);
		pipeSegment=new PlumbingItem("pipe8",false,false,LARGE);
		c2=new PlumbingProject(pipeSegment);
		b.connectTo(c2,1,1);
		c2.connectTo(c1,1,2);
 
	 	pipeSegment=new PlumbingItem("pipe12",false,false,LARGE);
		teeSegment=new PlumbingItem("tee5",true,true,LARGE);
		c1.connectTo(pipeSegment,1,1);
		c1=c1.getConnection(1);
		c1.connectTo(teeSegment,1,1);
		c1=c1.getConnection(1);
		pipeSegment=new PlumbingItem("pipe9",false,false,LARGE);
		c2=new PlumbingProject(pipeSegment);
		s.connectTo(c2,1,1);
		c2.connectTo(c1,1,2);
		
	 	pipeSegment=new PlumbingItem("pipe13",false,false,LARGE);
		teeSegment=new PlumbingItem("tee6",true,true,LARGE);
		c1.connectTo(pipeSegment,1,1);
		c1=c1.getConnection(1);
		c1.connectTo(teeSegment,1,1);
		c1=c1.getConnection(1);
		pipeSegment=new PlumbingItem("pipe10",false,false,LARGE);
		c2=new PlumbingProject(pipeSegment);
		t.connectTo(c2,1,1);
		c2.connectTo(c1,1,2);
		
	 	pipeSegment=new PlumbingItem("pipe14",false,false,SMALL);
	 	sewerOut=new PlumbingProject(pipeSegment);
		c1.connectTo(pipeSegment,1,1);
	}
	
	
	public void calculateFlow()
	{
		sewerIn.calculateFlow(0);   
	}
	
	
	public void turnOnBasin()
	{
		basin.setThroughFlow(true);
	 	calculateFlow();
   	}
	
	
	public void turnOnShower()
	{
	 	shower.setThroughFlow(true);
	 	calculateFlow();
   	}
	
	
	public void flushToilet()
	{
	 	toilet.setThroughFlow(true);
	 	calculateFlow();
   	}
	
	
	public void paint(Graphics g)
	{
		inFlowTF1.setText("Available "+Math.min(basin.getInFlowRate1(),basin.getInFlowCapacity1()));
		inFlowTF2.setText("Available "+Math.min(shower.getInFlowRate1(),shower.getInFlowCapacity1()));
		inFlowTF3.setText("Available "+Math.min(toilet.getInFlowRate1(),toilet.getInFlowCapacity1()));
		outFlowTF1.setText("Actual "+basin.getOutFlowRate1());
		outFlowTF2.setText("Actual "+shower.getOutFlowRate1());
		outFlowTF3.setText("Actual "+toilet.getOutFlowRate1());
		
		g.drawString("Total clean water entering system:" + cleanWaterIn.getItem().getOutFlowRate1(),20,200);
		g.drawString("Total clean water leaving system:" + cleanWaterOut.getItem().getOutFlowRate1(),20,220);
		g.drawString("Total sewage entering system:" + sewerIn.getItem().getOutFlowRate1(),20,240);
		g.drawString("Total sewage leaving system:" + sewerOut.getItem().getOutFlowRate1(),20,260);
		super.paint(g);
		
		if (basin.getOutFlowRate1()>sewerOut.getItem().getOutFlowRate1())
		{
			g.drawString("Basin has overflowed!!!",20,300);
		}
		if (shower.getOutFlowRate1()>sewerOut.getItem().getOutFlowRate1())
		{
			g.drawString("Shower has overflowed!!!",20,320);
		}
		if (toilet.getOutFlowRate1()>sewerOut.getItem().getOutFlowRate1())
		{
			g.drawString("Toilet has overflowed!!!",20,340);
		}
	}
}


Can anyone see what may be causing this? The other classes haven't been updated. They're the same as a few posts up.
Was This Post Helpful? 0
  • +
  • -

#12 skin__  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 20-May 08

Re: Null Pointer Exception

Posted 11 September 2008 - 05:47 PM

BUMP:
Anyone?
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Null Pointer Exception

Posted 11 September 2008 - 07:22 PM

View Postskin__, on 11 Sep, 2008 - 05:47 PM, said:

BUMP:
Anyone?

There are no problem with gDrawString() they are doing their job
add a System.out.println they are doing what they are supposed to do this means the values returned by getFlowRate(), get FlowRate1(), get...
The problem is not a graphic problem at all it is in the logic of your program

This post has been edited by pbl: 11 September 2008 - 08:17 PM

Was This Post Helpful? 0
  • +
  • -

#14 skin__  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 20-May 08

Re: Null Pointer Exception

Posted 13 September 2008 - 02:07 AM

Sorry to bump this again, but I can't see where my code is wrong. Can anyone help me?
Was This Post Helpful? 0
  • +
  • -

#15 skin__  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 20-May 08

Re: Null Pointer Exception

Posted 13 September 2008 - 04:55 PM

No one can see anything?
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3