Results 1 to 3 of 3
  1. #1
    quaffle97 is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Unhappy NullPointerException when trying to paint

    I have a little program i'm workin on, and it keeps complaining about nullPointerExceptions. Also, every once in a while, the program just draws a white screen instead of what i want it to print.

    Exception in thread "main" java.lang.NullPointerException
    at Cell.draw(InitClass.java:206)
    at MyGrid.drawCells(InitClass.java:125)
    at MainClass.<init>(InitClass.java:40)
    at InitClass.main(InitClass.java:11)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at MyPanel.paint(InitClass.java:87)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JLayeredPane.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    at javax.swing.RepaintManager$PaintManager.paintDoubl eBuffered(Unknown S
    ource)
    at javax.swing.RepaintManager$PaintManager.paint(Unkn own Source)
    at javax.swing.RepaintManager.paint(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at java.awt.GraphicsCallback$PaintCallback.run(Unknow n Source)
    at sun.awt.SunGraphicsCallback.runOneComponent(Unknow n Source)
    at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
    at java.awt.Container.paint(Unknown Source)
    at java.awt.Window.paint(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unkno wn Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unkno wn Source)
    at javax.swing.RepaintManager.seqPaintDirtyRegions(Un known Source)
    at javax.swing.SystemEventQueueUtilities$ComponentWor kRequest.run(Unknow
    n Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

    Here's the code, InitClass is what you're supposed to run.

    Java Code:
    
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import javax.swing.*;
    
    public class InitClass{
    	static MainClass TheMainClass;
    	
    	public static void main(String[] args){
    		TheMainClass=new MainClass();
    	}
    	
    }
    
    
    
    
    
     class MainClass implements ActionListener{
    	Timer MyTimer;
    	final int timerDelay=0;
    	final int gridWidth=10;
    	final int gridHeight=10;
    	final int cellWidth=24;
    	final int cellHeight=24;
    	
    	int x;
    	MyGrid TheGrid;
    	
    	MyFrame MainWindow;
    	
    	public MainClass(){
    		MainWindow = new MyFrame();
    		MyTimer = new Timer(timerDelay,this);
    		MyTimer.start();
    		TheGrid = new MyGrid(gridWidth,gridHeight,cellWidth,cellHeight);
    		
    		Graphics g=MainWindow.finalPic.getGraphics();
    		g.drawImage(TheGrid.drawCells(),0,0,null);
    		MainWindow.repaint();
    	}
    	
    	public void actionPerformed(ActionEvent e){
    		
    		
    	}
    	
    	
    }
    
     class MyFrame extends JFrame {
      
       public   BufferedImage finalPic;
       MyPanel drawPane;
       
       public BufferedImage getBufferImage(){
       	  return finalPic;   	   
       }
       
        public MyFrame() {  	    
    
            drawPane= new MyPanel();
        	add(drawPane);
        	
            setTitle("MyFrame");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setSize(700, 700);
            setLocationRelativeTo(null);
            setVisible(true);
            setResizable(false);
            
            finalPic = new BufferedImage(this.getWidth()-this.getInsets().left-this.getInsets().right,this.getHeight()-this.getInsets().top-this.getInsets().bottom,BufferedImage.TYPE_4BYTE_ABGR);
            Graphics g = finalPic.getGraphics();
            g.setColor(Color.black);
            g.fillRect(0,0,finalPic.getWidth(),finalPic.getHeight());
            
        }
        
    }
    
     class MyPanel extends JPanel implements ImageObserver{
     
    	public void paint (Graphics g){
    		
    		
     	 	g.drawImage(InitClass.TheMainClass.MainWindow.finalPic,0,0,this);
      	 	
    	
    	}
    	
    }
    
    
    class MyGrid {
    	Cell[][] CellGrid;
    	final int TOP=1;
    	final int RIGHT=2;
    	final int BOTTOM=3;
    	final int LEFT=4;
    	int gridWidth;
    	int gridHeight;
    	int cellWidth;
    	int cellHeight;
    	public MyGrid(int width,int height,int cellWidth,int cellHeight){
    		CellGrid=new Cell[width][height];
    		this.cellWidth=cellWidth;
    		this.cellHeight=cellHeight;
    		gridWidth=width;
    		gridHeight=height;
    		
    		for(int i = 0; i < width; i++){
    			for(int j = 0;j < height;j++){
    			CellGrid[i][j]=new Cell(i,j,cellWidth,cellHeight);
    			}	
    		}
    	}
    	
    	public BufferedImage drawCells(){
    		BufferedImage pic=new BufferedImage(gridWidth*cellWidth,gridHeight*cellHeight,BufferedImage.TYPE_4BYTE_ABGR);
    		BufferedImage[][] picArray = new BufferedImage[gridWidth][gridHeight];
    		for(int i = 0; i < gridWidth; i++){
    			for(int j = 0; j < gridHeight; j++){
    				
    			 picArray[i][j]=CellGrid[i][j].draw();	
    			 Graphics g=pic.getGraphics();
    			 g.drawImage(CellGrid[i][j].draw(),cellWidth*i,cellHeight*j,null);
    			}
    			
    		}
    		return pic;
    	}
    	
    	public Cell getRelativeCell(int relX,int relY,Cell theCell){
    		int newX=theCell.x()+relX;
    		int newY=theCell.y()+relY;
    		if(newX<0||newX>=gridHeight||newY<0||newY>=gridHeight)
    			return null;
    		else{
    		Cell temp= CellGrid[newX][newY];
    		return temp;
    		}
    		
    	}
    	
    }
    
    class Coordinates{
    	public int x;
    	public int y;
    }
    
    class Cell{
    	int gridX;
    	int gridY;
    	int cellWidth;
    	int cellHeight;
    	final int TOP=1;
    	final int RIGHT=2;
    	final int BOTTOM=3;
    	final int LEFT=4;
    	Boolean state;
    	
    	Coordinates enumToCoordinates(int dir){
    		Coordinates temp=new Coordinates();		
    		
    		if(dir%2==1){
    			temp.x=0;
    			if(dir==1)temp.y=-1;
    			else temp.y=1;
    		}
    		else{
    			temp.y=0;
    			if(dir==2)temp.x=1;
    			else temp.x=-1;
    		}
    		return temp;
    	}
    	
    	public Cell(int x,int y,int width,int height){
    		cellWidth=width;
    		cellHeight=height;
    		gridX=x;
    		gridY=y;
    	}
    	
    	public int x(){
    		return gridX;
    	}
    	
    	public int y(){
    		return gridY;
    	}
    	
    	public BufferedImage draw(){
    		BufferedImage CellPic = new BufferedImage(cellWidth,cellHeight,BufferedImage.TYPE_4BYTE_ABGR);
    		Graphics g = CellPic.getGraphics();
    		g.setColor(Color.white);
    		g.fillRect(1,1,cellWidth,cellHeight);
    		g.setColor(Color.gray);
    		g.drawRect(0,0,cellWidth-1,cellHeight-1);
    		for(int i = TOP;i <= BOTTOM;i++){
    			
    			
    			
    			InitClass.TheMainClass.TheGrid.getRelativeCell(enumToCoordinates(i).x,enumToCoordinates(i).y,this);
    			
    		}
    		
    		return CellPic;
    	}
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    edit: misread the code. The advice to go through the tutorial still stands: you're overriding the wrong method.

    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db
    Last edited by DarrylBurke; 03-07-2011 at 05:46 AM.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Also go through
    Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    Swing GUIs should be constructed and launched on the EDT.

    And drawPane is instantiated before finalPic. That could be the root source of the NPEs.

    db

Similar Threads

  1. Paint????
    By seanfmglobal in forum New To Java
    Replies: 3
    Last Post: 02-15-2011, 08:00 AM
  2. paint method
    By larry_d1990 in forum Advanced Java
    Replies: 1
    Last Post: 01-09-2011, 05:45 PM
  3. Some confusions with paint()
    By kendaop in forum Java Applets
    Replies: 1
    Last Post: 01-24-2009, 12:23 AM
  4. finished paint!
    By diggitydoggz in forum New To Java
    Replies: 3
    Last Post: 01-04-2009, 10:33 AM
  5. other than paint repaint
    By amith in forum Java 2D
    Replies: 1
    Last Post: 07-01-2008, 11:39 PM

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
  •