null pointer exception

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • true blue
    New Member
    • Jul 2007
    • 8

    #1

    null pointer exception

    hey ppl
    plz can any one help me with this code im gettin a null pointer exception when i
    use graphics class object (g.drawString)i n action performed functio eventhough i hava declared the object outside init and made it global plz bear im relatively new to java
    import java.awt.*;
    import java.awt.event. *;
    import java.applet.*;
    public class calander extends Applet implements ActionListener, MouseMotionList ener
    {
    Label l1,l2,l3,l4;
    TextField t1,t2,t3;
    Choice c;
    int x,y,month,year, sums=0,ly=0,y2, i,jj,b,x1,y1,da ys,l;
    String d="";
    Panel p0,p1,p2,p3,p4, p5;
    Button b1;
    Graphics g=getGraphics() ;
    public void init()
    {
    setLayout(null) ;
    setBackground(C olor.black);
    b1=new Button("SHOW");
    l1=new Label("ENTER YEAR after 2001:");
    l2=new Label("CHOOSE A MONTH : ");
    l3=new Label("X");
    l4=new Label("Y");
    l1.setForegroun d(Color.cyan);
    l2.setForegroun d(Color.cyan);
    l3.setForegroun d(Color.cyan);
    l4.setForegroun d(Color.cyan);
    Font f=new Font("MS COMIC SANS",Font.ITAL IC,18);
    b1.setFont(f);
    b1.setBackgroun d(Color.black);
    b1.setForegroun d(Color.cyan);
    l1.setFont(f);
    l2.setFont(f);
    t1=new TextField(10);
    t2=new TextField(10);
    t3=new TextField(5);
    c=new Choice();
    c.setBackground (Color.black);
    c.setForeground (Color.cyan);
    Font f1=new Font("MONOTYPE CORSIVA",Font.I TALIC,14);
    c.setFont(f1);
    c.addItem("JANU ARY");
    c.addItem("FEBR UARY");
    c.addItem("MARC H");
    c.addItem("APRI L");
    c.addItem("MAY" );
    c.addItem("JUNE ");
    c.addItem("JULY ");
    c.addItem("AUGU ST");
    c.addItem("SEPT EMBER");
    c.addItem("OCTO BER");
    c.addItem("NOVE MBER");
    c.addItem("DECE MBER");
    p0=new Panel();
    p1=new Panel();
    p2=new Panel();
    p3=new Panel();
    p1.setLayout(ne w GridLayout(1,2) );
    p1.setBackgroun d(Color.black);
    p1.add(l2);
    p1.add(c);
    p1.setBounds(10 0,20,450,60);
    add(p1);
    p2.setLayout(ne w GridLayout(1,2) );
    p2.setBackgroun d(Color.black);
    p2.add(l1);
    p2.add(t3);
    p2.setBounds(56 6,20,420,60);
    add(p2);
    p0.setBackgroun d(Color.black);
    p0.setLayout(ne w GridLayout(2,2) );
    p0.add(l3);
    p0.add(t1);
    p0.add(l4);
    p0.add(t2);
    p0.setBounds(10 ,10,70,50);
    add(p0);
    p3.setBackgroun d(Color.black);
    p3.add(b1);
    p3.setBounds(40 0,600,200,100);
    add(p3);
    resize(1024,768 );
    setVisible(true );
    b1.addActionLis tener(this);
    addMouseMotionL istener(this);
    }
    public void paint(Graphics g)
    {
    g.setColor(Colo r.green);
    Font f2=new Font("ARIAL",Fo nt.BOLD,24);
    g.setFont(f2);
    g.drawString("S ",50,122);
    g.drawString("M ",200,122);
    g.drawString("T ",350,122);
    g.drawString("W ",500,122);
    g.drawString("T ",650,122);
    g.drawString("F ",800,122);
    g.drawString("S ",950,122);
    }
    public void actionPerformed (ActionEvent e)
    {
    if(e.getSource( )==b1)
    {
    String f=c.getSelected Item();
    System.out.prin tln(f);
    if(f=="JANUARY" )
    month=1;
    if(f=="FEBRUARY ")
    month=2;
    if(f=="MARCH")
    month=3;
    if(f=="APRIL")
    month=4;
    if(f=="MAY")
    month=5;
    if(f=="JUNE")
    month=6;
    if(f=="JULY")
    month=7;
    if(f=="AUGUST")
    month=8;
    if(f=="SEPTEMBE R")
    month=9;
    if(f=="OCTOBER" )
    month=10;
    if(f=="NOVEMBER ")
    month=11;
    if(f=="DECEMBER ")
    month=12;
    }
    year=Integer.pa rseInt(t3.getTe xt());
    System.out.prin tln(year);
    y2=year-2001;
    for(i=2001;i<=y ear-1;i++)
    {
    if(i%4==0)
    ly++;
    else
    continue;
    }
    sums=(365*y2)+l y;
    for(int jj=1;jj<=month-1;jj++)
    {
    if(jj==1||jj==3 ||jj==5||jj==7| |jj==8||jj==10| |jj==12)
    sums+=31;
    if((jj==2)&&(ye ar%4==0))
    sums+=29;
    if((jj==2)&&(ye ar%4!=0))
    sums+=28;
    if(jj==4||jj==6 ||jj==9||jj==11 )
    sums+=30;
    }
    System.out.prin tln(sums);
    if(month==1||mo nth==3||month== 5||month==7||mo nth==8||month== 10||month==12)
    days=31;
    if((month==2)&& (year%4==0))
    days=29;
    if((month==2)&& (year%4!=0))
    days=28;
    if(month==4||mo nth==6||month== 9||month==11)
    days=30;
    b=sums%7;
    System.out.prin tln(days);
    System.out.prin tln(b);
    switch(b)
    {
    case 0:
    l=1;
    x1=200;
    y1=212;
    if(x1<=950)
    {

    d=String.valueO f(l);
    System.out.prin tln(d);
    g.drawString(d, x1,y1);
    System.out.prin tln(b);
    x1+=150;
    l++;
    d="";
    }
    x1=50;
    y1=302;
    if(x1<=950)
    {

    d=String.valueO f(l);
    g.drawString(d, x1,y1);
    x1+=150;
    l++;
    d="";
    }
    x1=50;
    y1=392;
    if(x1<=950)
    {

    d=String.valueO f(l);
    g.drawString(d, x1,y1);
    x1+=150;
    l++;
    d="";
    }
    x1=50;
    y1=482;
    if(x1<=950)
    {

    d=String.valueO f(l);
    g.drawString(d, x1,y1);
    x1+=150;
    l++;
    d="";
    }
    x1=50;
    y1=572;
    while(l<=days)
    {

    g.drawString(d, x1,y1);
    x1+=150;
    l++;
    d="";
    }
    break;
    case 1:
    l=1;
    x1=350;
    y1=212;
    if(x1<=950)
    {

    d=String.valueO f(l);
    g.drawString(d, x1,y1);
    x1+=150;
    l++;
    d="";
    }
    x1=50;
    y1=302;
    if(x1<=950)
    {

    d=String.valueO f(l);
    g.drawString(d, x1,y1);
    x1+=150;
    l++;
    d="";
    }
    x1=50;
    y1=392;
    if(x1<=950)
    {

    d=String.valueO f(l);
    g.drawString(d, x1,y1);
    x1+=150;
    l++;
    d="";
    }
    x1=50;
    y1=482;
    if(x1<=950)
    {

    d=String.valueO f(l);
    g.drawString(d, x1,y1);
    x1+=150;
    l++;
    d="";
    }
    x1=50;
    y1=572;
    while(l<=days)
    {

    g.drawString(d, x1,y1);
    x1+=150;
    l++;
    d="";
    }
    break;
    case 2:
    l=1;
    x1=500;
    y1=212;
    if(x1<=950)
    {

    d=String.valueO f(l);
    g.drawString(d, x1,y1);
    x1+=150;
    l++;
    d="";
    }
    x1=50;
    y1=302;
    if(x1<=950)
    {

    d=String.valueO f(l);
    g.drawString(d, x1,y1);
    x1+=150;
    l++;
    d="";
    }
    x1=50;
    y1=392;
    if(x1<=950)
    {

    d=String.valueO f(l);
    g.drawString(d, x1,y1);
    x1+=150;
    l++;
    d="";
    }
    x1=50;
    y1=482;
    if(x1<=950)
    {

    d=String.valueO f(l);
    g.drawString(d, x1,y1);
    x1+=150;
    l++;
    d="";
    }
    x1=50;
    y1=572;
    while(l<=days)
    {

    g.drawString(d, x1,y1);
    x1+=150;
    l++;
    d="";
    }
    break;
    case 3:
    l=1;
    x1=650;
    y1=212;
    if(x1<=950)
    {

    d=String.valueO f(l);
    g.drawString(d, x1,y1);
    x1+=150;
    l++;
    d="";
    }
    x1=50;
    y1=302;
    if(x1<=950)
    {

    d=String.valueO f(l);
    g.drawString(d, x1,y1);
    x1+=150;
    l++;
    d="";
    }
    x1=50;
    y1=392;
    if(x1<=950)
    {

    d=String.valueO f(l);
    g.drawString(d, x1,y1);
    x1+=150;
    l++;
    d="";
    }
    x1=50;
    y1=482;
    if(x1<=950)
    {

    d=String.valueO f(l);
    g.drawString(d, x1,y1);
    x1+=150;
    l++;
    d="";
    }
    x1=50;
    y1=572;
    while(l<=days)
    {

    g.drawString(d, x1,y1);
    x1+=150;
    l++;
    d="";
    }
    break;
    case 4:
    l=1;
    x1=800;
    y1=212;
    if(x1<=950)
    {

    d=String.valueO f(l);
    g.drawString(d, x1,y1);
    x1+=150;
    l++;
    d="";
    }
    x1=50;
    y1=302;
    if(x1<=950)
    {

    d=String.valueO f(l);
    g.drawString(d, x1,y1);
    x1+=150;
    l++;
    d="";
    }
    x1=50;
    y1=392;
    if(x1<=950)
    {

    d=String.valueO f(l);
    g.drawString(d, x1,y1);
    x1+=150;
    l++;
    d="";
    }
    x1=50;
    y1=482;
    if(x1<=950)
    {

    d=String.valueO f(l);
    g.drawString(d, x1,y1);
    x1+=150;
    l++;
    d="";
    }
    x1=50;
    y1=572;
    while(l<=days)
    {

    g.drawString(d, x1,y1);
    x1+=150;
    l++;
    d="";
    }
    break;
    case 5:
    l=1;
    x1=950;
    y1=212;
    if(x1<=950)
    {

    d=String.valueO f(l);
    g.drawString(d, x1,y1);
    x1+=150;
    l++;
    d="";
    }
    x1=50;
    y1=302;
    if(x1<=950)
    {

    d=String.valueO f(l);
    g.drawString(d, x1,y1);
    x1+=150;
    l++;
    d="";
    }
    x1=50;
    y1=392;
    if(x1<=950)
    {

    d=String.valueO f(l);
    g.drawString(d, x1,y1);
    x1+=150;
    l++;
    d="";
    }
    x1=50;
    y1=482;
    if(x1<=950)
    {

    d=String.valueO f(l);
    g.drawString(d, x1,y1);
    x1+=150;
    l++;
    d="";
    }
    x1=50;
    y1=572;
    while(l<=days)
    {

    g.drawString(d, x1,y1);
    x1+=150;
    l++;
    d="";
    }
    break;
    default:
    l=1;
    x1=50;
    y1=212;
    if(x1<=950)
    {

    d=String.valueO f(l);
    g.drawString(d, x1,y1);
    x1+=150;
    l++;
    d="";
    }
    x1=50;
    y1=302;
    if(x1<=950)
    {

    d=String.valueO f(l);
    g.drawString(d, x1,y1);
    x1+=150;
    l++;
    d="";
    }
    x1=50;
    y1=392;
    if(x1<=950)
    {

    d=String.valueO f(l);
    g.drawString(d, x1,y1);
    x1+=150;
    l++;
    d="";
    }
    x1=50;
    y1=482;
    if(x1<=950)
    {

    d=String.valueO f(l);
    g.drawString(d, x1,y1);
    x1+=150;
    l++;
    d="";
    }
    x1=50;
    y1=572;
    while(l<=days)
    {

    g.drawString(d, x1,y1);
    x1+=150;
    l++;
    d="";
    }
    break;
    }
    }

    public void mouseMoved(Mous eEvent m)
    {
    x=m.getX();
    y=m.getY();
    t1.setText(x+"" );
    t2.setText(y+"" );
    }
    public void mouseDragged(Mo useEvent m)
    {}
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Drawing in the AWT or Swing framework doesn't work like that; whenever
    something needs to be repainted AWT invokes your paint() method and passes
    is a properly initialized Graphics object. Use only that object for your painting
    actions. Don't just declare a Graphics object just to 'have one and keep the
    compiler's mouth shut'. It doesn't work that way.

    I suggest you'd read an AWT or Swing tutorial first before you attempt to code
    something up. Also read the API tutorial for the GregorianCalend ar; that class
    was designed and implemented for all sorts of calendar work; don't reinvent the
    weel. On top of that, read the API for the String class because you can't
    compare Strings the way you attempt to do it. Please don't just guess how
    Java classes should work according to your imagination. Read the documentation
    first and see how they really work. The way your code is set up now it is never
    going to work; really.

    kind regards,

    Jos

    ps. and please don't open more than one thread for a single problem.

    Comment

    • nickyeng
      Contributor
      • Nov 2006
      • 252

      #3
      i suggest put the coding in the code quote....

      kinda messy and untidy up there.

      Comment

      Working...