Results 1 to 6 of 6
  1. #1
    kipcorn91 is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default Nullpointer Exception???

    Hello

    I have been programming in Java for school projects for two months now
    Today I was experimentating with threads
    But when I'm running my code I get this error:

    XML Code:
    Exception in thread "Thread-2" java.lang.NullPointerException
    	at Graph.draw(MultiThreadTest.java:60)
    	at Graph.run(MultiThreadTest.java:52)
    I can't possebly imagine what I am doing wrong
    I would appriciate some help
    here is my code:

    XML Code:
    import javax.swing.*;
    import java.awt.*;
    //import java.awt.event.*;
    
    public class MultiThreadTest extends JFrame{
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		JFrame frame = new MultiThreadTest();
    		frame.setSize(1000, 600);
    		frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
    		frame.setTitle( "Graphs" );
    		JPanel paneel = new GraphPanel();
    		frame.setContentPane( paneel );
    		frame.setVisible( true );
    	}
    }
    
    class GraphPanel extends JPanel{
    	
    	Graph graph1;
    	
    	public GraphPanel(){
    		setLayout(null);
    		
    		if ( graph1 == null) {
    		graph1 = new Graph( getGraphics(), 200, 550, 10, 10);
    		graph1.start();
    		}		
    	}
    }
    
    class Graph extends Thread {
    	private int pauseTime, xOrigin, yOrigin, heigth = 0, stepSeize;
    	private Graphics graph;
    	private boolean clearance = true, direction = true;
    	
    	//Constructor
    	public Graph( Graphics g, int pauseTime, int xOrigin, int yOrigin, int stepSeize){
    		this.pauseTime = pauseTime;
    		this.xOrigin = xOrigin;
    		this.yOrigin = yOrigin;
    		this.graph = g;
    		this.stepSeize = stepSeize;
    	}
    	
    	//Run method
    	public void run() {
    		while ( clearance ){
    			draw();
    			pause( pauseTime );
    			changeHeigth();
    		}		
    	}
    	
    	//Drawing the bar
    	public void draw() {
    		graph.setColor( Color.red );
    		graph.fillRect(xOrigin, yOrigin - heigth, 50, heigth);
    	}
    	
    	//This method is used to change the total height of the bar
    	public void changeHeigth() {
    		
    		if ( heigth >= 0 && heigth <= 500 && direction == true ){
    			heigth += stepSeize;
    		}
    		
    		if ( heigth > 500 ){
    			direction = false;
    		}
    		
    		if ( heigth >= 0 && direction == false ){
    			heigth -= stepSeize;
    		}
    		
    		if ( heigth < 0 && direction == false ){
    			heigth = 0 ;
    			direction = true;
    		}
    	}
    
    	//pause or pause
    	public void pause ( int milisec ){
    		try {
    			Thread.sleep(milisec);
    		}
    		catch( InterruptedException e ) {}
    		}
    	
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Which is line 60?

    You can check, for example, whether graph is null in the draw() method by using System.out.println() to print its value at the start of that method.

    If it is null you have to look in your code at the place where you thought it was given a nonnull value and figure out why that didn't happen.

  3. #3
    kipcorn91 is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    this is line 60:

    XML Code:
    //Drawing the bar
    	public void draw() {
    		graph.setColor( Color.red ); // line 60
    		graph.fillRect(xOrigin, yOrigin - heigth, 50, heigth);
    	}
    but if I comment the statment
    XML Code:
    graph.setColor( Color.red )
    then the error occurs on line 61 :P

    and this is line 52:

    XML Code:
    public void run() {
    	while ( clearance ){
    		draw(); // line 52
    		pause( pauseTime );
    		changeHeigth();
    	}		
    }
    I have tried the
    XML Code:
    System.out.println(graph)
    , and yes it displayd
    XML Code:
    null

  4. #4
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    4

    Default

    Yeah, it's null because you're getting your Graphics object via getGraphics() call which as you're finding out does not return an object that persists throughout the program. In short, you don't do Swing graphics this way. Instead do your drawing in the JPanel's paintComponent as most any tutorial you read on this will tell you to do.

  5. #5
    kipcorn91 is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    Oké, but how do I do that becaus I have think I have to paint in my thread?

  6. #6
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    4

    Default

    Quote Originally Posted by kipcorn91 View Post
    Oké, but how do I do that becaus I have think I have to paint in my thread?
    Myself, I'd use a Swing Timer, but if you must use a separate thread, you would change class fields/properties from within the thread, and then call repaint on the JPanel. The JPanel's paintComponent method would use the changed properties to figure out what to draw where. Check out this forum and others and you'll find lots of examples of this as it is a common problem.

Similar Threads

  1. Reading from file - NullPointer exception
    By Sage25 in forum New To Java
    Replies: 5
    Last Post: 07-08-2010, 05:34 PM
  2. NullPointer exception
    By bdario1 in forum New To Java
    Replies: 15
    Last Post: 03-17-2010, 04:44 AM
  3. NullPointer.exception in main (arrays)
    By Jana in forum New To Java
    Replies: 5
    Last Post: 02-20-2009, 06:41 PM
  4. nullpointer exception in jsp
    By fiero in forum JavaServer Pages (JSP) and JSTL
    Replies: 6
    Last Post: 11-07-2008, 01:44 PM
  5. NullPointer Exception
    By Preethi in forum New To Java
    Replies: 8
    Last Post: 02-06-2008, 03:40 PM

Posting Permissions

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