Results 1 to 2 of 2
  1. #1
    alaa is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default java.lang.NullPointerException

    i write code to drow graph

    but i have this exception

    "AWT-EventQueue-0" java.lang.NullPointerException

    what this and how can i avoid it

    my programe act good id size ==4

    but if i change to 5 for example
    "AWT-EventQueue-0" java.lang.NullPointerException

    this my code

    XML Code:
                      import java.util.ArrayList;
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    
    public class TestPanelDrawing extends JFrame {
    
            public int arr[][] = {{0, 0, 6, 5, 0, 2, 0}, {0, 0, -1, -1, 0, 2, 0}, {6, -1, 0, 3, 3, 2, 0}, {5, -1, 3, 0, 5, 2, 0}, {0, 0, 3, 5, 0, 2, 0}, {2, 2, 2, 2, 2, 0, 7}, {0, 0, 0, 0, 0, 7, 0}};
            NewPanel s = new NewPanel();
    
            public TestPanelDrawing() {
            add(new NewPanel());
            s.enterDest(arr);
    
            }
    
            public static void main(String[] args) {
            TestPanelDrawing frame = new TestPanelDrawing();
            frame.setTitle("TestPanelDrawing");
            frame.setLocationRelativeTo(null); // Center the frame
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(600, 400);
            frame.setVisible(true);
            }
    }
    
    class NewPanel extends JPanel {
    
          // public int  arr[][]={{0,7,6,5,0,2,0},{0,0,-1,-1,0,2,0},{6,-1,0,3,3,2,0},{5,-1,3,0,5,2,0},{0,0,3,5,0,2,0},{2,2,2,2,2,0,7},{0,0,0,0,0,7,0}};
            
        int[][] arr;
         int size = 5;
           
            public void enterDest(int y[][]) {
            arr = y;
            }
           
           
           
           
            protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Font r = new Font("SansSerif", Font.BOLD, 16);
            g.setFont(r);
            
           
            if (size== 4) {
    
                    Graphics2D g2 = (Graphics2D) g;
                    super.paintComponent(g);
                    Point2D.Double point = new Point2D.Double(5, 5);
    
                    int x0 = 30;
                    int x1 = 150;
                    int y0 = 20;
                    int y1 = 150;
                    int x[] = {x0, x1};
                    int y[] = {y0, y1};
                    ArrayList<Ellipse2D.Double> vert = new ArrayList<Ellipse2D.Double>();
    
                    Ellipse2D.Double a = new Ellipse2D.Double(x0 + 5, y0, 15, 15);
                    Ellipse2D.Double b = new Ellipse2D.Double(x0 + 5, y1, 15, 15);
                    Ellipse2D.Double c = new Ellipse2D.Double(x1 + 5, y0, 15, 15);
                    Ellipse2D.Double d = new Ellipse2D.Double(x1 + 5, y1, 15, 15);
                    vert.add(a);
                    vert.add(b);
                    vert.add(c);
                    vert.add(d);
    
                    g.drawString(" A ", x0 + 15, y0);
                    g.drawString(" B ", x0 + 15, y1);
                    g.drawString(" C ", x1, y0);
                    g.drawString(" D ", x1 + 5, y1);
    
                    vert.add(a);
                    vert.add(b);
                    vert.add(c);
                    vert.add(d);
    
                    g2.fill(new Ellipse2D.Double(x0, y0, 15, 15));
                    g2.fill(new Ellipse2D.Double(x0, y1, 15, 15));
                    g2.fill(new Ellipse2D.Double(x1, y0, 15, 15));
                    g2.fill(new Ellipse2D.Double(x1, y1, 15, 15));
    
                    for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                            g.drawLine((int) vert.get(i).getX(), (int) vert.get(i).getY(),
                                    (int) vert.get(j).getX(), (int) vert.get(j).getY());
                    }
                    }
                    
                    
                    
            }
            
            
        	if (size==5){
        
        	Graphics2D g2 = (Graphics2D)g;
          super.paintComponent(g);
          Point2D.Double point = new Point2D.Double(5,5);
         // Ellipse2D.Double a=  new Ellipse2D.Double(6, 6, 20, 20);
          int x0=30;
          int x1=150;
          int x2=230;
          int y0=20;
          int y1=150;
          int y2=75;
          int x[]={x0,x1,x2};
          int y[]={y0,y1,y2};
        	ArrayList<Ellipse2D.Double> vert = new ArrayList<Ellipse2D.Double>();
        	
        Ellipse2D.Double a=  new Ellipse2D.Double(x0+5, y0, 15, 15);
        Ellipse2D.Double b=  new Ellipse2D.Double(x0+5, y1, 15, 15);
        Ellipse2D.Double c=  new Ellipse2D.Double(x1+5, y0, 15, 15);
        Ellipse2D.Double d=  new Ellipse2D.Double(x1+5, y1, 15, 15);
        Ellipse2D.Double e=  new Ellipse2D.Double(x2+5, y2, 15, 15);
       
        vert.add(a);
          vert.add(b);
          vert.add(c);
          vert.add(d);
          vert.add(e);
          
       // g.drawLine((int)a.getX(), (int)a.getY(),(int) b.getX(),(int) b.getY());	
           	
       // for(int f=0;f<5;f++)
        	g.drawString(" A ",x0+15,y0);
        		g.drawString(" B ",x0+15,y1);
        			g.drawString(" C ",x1,y0);
        				g.drawString(" D ",x1+5,y1);
        	              g.drawString(" E ",x2+5,y2);
        	
          
          
          	
             g2.fill(new Ellipse2D.Double(x0, y0, 15, 15));
             g2.fill(new Ellipse2D.Double(x0, y1, 15, 15));
            g2.fill(new Ellipse2D.Double(x1, y0, 15, 15));
            g2.fill(new Ellipse2D.Double(x1, y1, 15, 15));
            g2.fill(new Ellipse2D.Double(x2, y2, 15, 15));
         
           for(int i=0;i<5;i++){
          for (int j=0;j<5;j++){
          if (arr[i][j]!=0&&arr[j][i]!=-1)
         	g.drawLine((int)vert.get(i).getX(), (int)vert.get(i).getY(),(int)vert.get(j).getX(),(int)vert.get(j).getY());
           
           	
           }	 
           }
        	}
        	
          //////////////
          
          	if (size==6){
        
        	Graphics2D g2 = (Graphics2D)g;
          super.paintComponent(g);
          Point2D.Double point = new Point2D.Double(5,5);
         // Ellipse2D.Double a=  new Ellipse2D.Double(6, 6, 20, 20);
          int x0=30;
          int x1=150;
          int x2=300;
          int y0=20;
          int y1=150;
          
          int x[]={x0,x1,x2};
          int y[]={y0,y1};
        	ArrayList<Ellipse2D.Double> vert = new ArrayList<Ellipse2D.Double>();
        	
        Ellipse2D.Double a=  new Ellipse2D.Double(x0+5, y0, 15, 15);
        Ellipse2D.Double b=  new Ellipse2D.Double(x0+5, y1, 15, 15);
        Ellipse2D.Double c=  new Ellipse2D.Double(x1+5, y0, 15, 15);
        Ellipse2D.Double d=  new Ellipse2D.Double(x1+5, y1, 15, 15);
        Ellipse2D.Double e=  new Ellipse2D.Double(x2+5, y0, 15, 15);
        Ellipse2D.Double f=  new Ellipse2D.Double(x2+5, y1, 15, 15);
        vert.add(a);
          vert.add(b);
          vert.add(c);
          vert.add(d);
          vert.add(e);
          vert.add(f);
       
        	g.drawString(" A ",x0+15,y0);
        		g.drawString(" B ",x0+15,y1);
        			g.drawString(" C ",x1,y0);
        				g.drawString(" D ",x1+5,y1);
        	              g.drawString(" E ",x2+5,y0);
        	               g.drawString(" F ",x2+5,y1);
          
          
          	
             g2.fill(new Ellipse2D.Double(x0, y0, 15, 15));
             g2.fill(new Ellipse2D.Double(x0, y1, 15, 15));
            g2.fill(new Ellipse2D.Double(x1, y0, 15, 15));
            g2.fill(new Ellipse2D.Double(x1, y1, 15, 15));
            g2.fill(new Ellipse2D.Double(x2, y0, 15, 15));
           g2.fill(new Ellipse2D.Double(x2, y1, 15, 15));
           
          
           
           
           for(int i=0;i<6;i++){
          for (int j=0;j<6;j++){
          if (arr[i][j]!=0&&arr[j][i]!=-1)
         	g.drawLine((int)vert.get(i).getX(), (int)vert.get(i).getY(),(int)vert.get(j).getX(),(int)vert.get(j).getY());
           
           	
           }	 
           }
        	}
          
          /////////
          	if (size==7){
        
        	Graphics2D g2 = (Graphics2D)g;
          super.paintComponent(g);
          Point2D.Double point = new Point2D.Double(5,5);
         // Ellipse2D.Double a=  new Ellipse2D.Double(6, 6, 20, 20);
          int x0=30;
          int x1=150;
          int x2=300;
          int x4=450;
          int y0=20;
          int y1=150;
          int y2=75;
          
          int x[]={x0,x1,x2,x4};
          int y[]={y0,y1,y2};
        	ArrayList<Ellipse2D.Double> vert = new ArrayList<Ellipse2D.Double>();
        	
        Ellipse2D.Double a=  new Ellipse2D.Double(x0+5, y0, 15, 15);
        Ellipse2D.Double b=  new Ellipse2D.Double(x0+5, y1, 15, 15);
        Ellipse2D.Double c=  new Ellipse2D.Double(x1+5, y0, 15, 15);
        Ellipse2D.Double d=  new Ellipse2D.Double(x1+5, y1, 15, 15);
        Ellipse2D.Double e=  new Ellipse2D.Double(x2+5, y0, 15, 15);
        Ellipse2D.Double f=  new Ellipse2D.Double(x2+5, y1, 15, 15);
        Ellipse2D.Double t=  new Ellipse2D.Double(x4+5, y2, 15, 15);
        
          vert.add(a);
          vert.add(b);
          vert.add(c);
          vert.add(d);
          vert.add(e);
          vert.add(f);
          vert.add(t);
       
        	g.drawString(" A ",x0+15,y0);
        		g.drawString(" B ",x0+15,y1);
        			g.drawString(" C ",x1,y0);
        				g.drawString(" D ",x1+5,y1);
        	              g.drawString(" E ",x2+5,y0);
        	               g.drawString(" F ",x2+5,y1);
                            g.drawString(" G ",x4+5,y2);
          
          	
             g2.fill(new Ellipse2D.Double(x0, y0, 15, 15));
             g2.fill(new Ellipse2D.Double(x0, y1, 15, 15));
            g2.fill(new Ellipse2D.Double(x1, y0, 15, 15));
            g2.fill(new Ellipse2D.Double(x1, y1, 15, 15));
            g2.fill(new Ellipse2D.Double(x2, y0, 15, 15));
           g2.fill(new Ellipse2D.Double(x2, y1, 15, 15));
           g2.fill(new Ellipse2D.Double(x4, y2, 15, 15));
        
            
           for(int i=0;i<7;i++){
          for (int j=0;j<7;j++){
          if (arr[i][j]!=0&&arr[j][i]!=-1){
          
         	g.drawLine((int)vert.get(i).getX(), (int)vert.get(i).getY(),(int)vert.get(j).getX(),(int)vert.get(j).getY());
            
         }
           }	 
           }
        	}
            
            }
    }

  2. #2
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    NullPointerException means you tried to do something with a variable that was null, i.e. not initialised. The full stack trace for the NullPointerException will tell you which line in the code it occurred.

    As it happens, your problem is that in your TestPanelDrawing constructor, you add an anonymous NewPanel (with null array), then set the array into the class field NewPanel (which doesn't get used).

    Change add(new NewPanel()); to add(s); in your TestPanelDrawing constructor.

Similar Threads

  1. Replies: 15
    Last Post: 01-13-2011, 06:28 PM
  2. java.lang.NullPointerException
    By peterhabe in forum New To Java
    Replies: 5
    Last Post: 08-27-2010, 08:43 PM
  3. java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 3
    Last Post: 02-28-2009, 05:41 AM
  4. java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 1
    Last Post: 02-27-2009, 12:36 PM
  5. java.lang.NullPointerException
    By Felissa in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 06:02 AM

Posting Permissions

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