NullPointerException打印到数组java小程序

NullPointerException打印到数组java小程序,java,user-interface,applet,calendar,nullpointerexception,Java,User Interface,Applet,Calendar,Nullpointerexception,这有两个主要问题。本文底部最后一个代码块是NullPointerException。我希望它在每次检测到某个特定日期发生的事件时都添加一个* 其次,我似乎无法让日历回到一个月前,并在适当的日期开始。。。这变得很烦人 import java.awt.*; import java.applet.*; import java.awt.event.*; public class Calendar extends Applet implements ActionListener{ /** * */

这有两个主要问题。本文底部最后一个代码块是NullPointerException。我希望它在每次检测到某个特定日期发生的事件时都添加一个*

其次,我似乎无法让日历回到一个月前,并在适当的日期开始。。。这变得很烦人

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Calendar extends Applet implements ActionListener{

/**
 * 
 */
private static final long serialVersionUID = 1L;
private Button next, last;
private Label month,showYear,sun,mon,tue,wed,thu,fri,sat;
private Label[] allDays= new Label[37];
private Label[] eventMarker = new Label[37]; 
private String[] months= {"January","February","March","April","May","June",
        "July","August","September","October","November","December"};
private String[] monthDays = {"31","29","31","30","31","30","31","31","30","31","30","31"};
private int current,year,dateHold,stringParse;
private final int MAX = 1000;//number of events
//arrays for data input from file
private int overallIndex = 0;
private String[] usrMonth= new String[MAX];
private String[] usrDay = new String[MAX];
private int[] usrYear = new int[MAX];
//private int[] usrTime = new int[MAX];
private String[] usrInfo = new String[MAX];
private TextField[] fields = new TextField[5];
private Label[] overFields = new Label[5];
private Button addApt ;


public void init(){
    setSize(1050,750);
    year = 2012;
    current = 0;//month in array
    next = new Button("Next");
    last = new Button("Last");
    month = new Label(months[current]);
    showYear = new Label("Year 2012");

    //day names on calendar
    sun = new Label("Sunday");
    mon = new Label("Monday");
    tue = new Label("Tuesday");
    wed = new Label("Wednesday");
    thu = new Label("Thursday");
    fri = new Label("Friday");
    sat = new Label("Saturday");
    //day numbers on calendar originally for January 2012
    String num;
    for(int i = 0; i < 37; i++){
        if(i > 30)
            allDays[i]=new Label(" ");
        else{
        num = "" + (i+1);
        allDays[i]=new Label(num);}
    }

    for(int i = 0; i < 37; i++){
        eventMarker[i] = new Label("|:");
    }

    addApt = new Button("Add Appointment");
    overFields[0] = new Label("Enter month by number.");
    overFields[1] = new Label("Enter the number date.");
    overFields[2] = new Label("Enter the year.");
    overFields[3] = new Label("Enter any information on appointment.");
    overFields[4] = new Label("Search appointments for key words here.");

    for (int i = 0; i < 5; i ++){
        fields[i] = new TextField("Dont forget to press Enter.",300);
    }


    //GUI add's

    //buttons
    add(next);
    add(last);
    add(addApt);
    //labels
    add(month);
    add(showYear);
    add(sun);
    add(mon);
    add(tue);
    add(wed);
    add(thu);
    add(fri);
    add(sat);
    //text fields and actionListener
    for(int i = 0; i < 5; i++){
        add(overFields[i]);
        add(fields[i]);
        fields[i].addActionListener(this);
    }

    for(int i = 0; i <37; i++)//Add array
        add(allDays[i]);


    //button listeners
    next.addActionListener(this);
    last.addActionListener(this);


}//end of init

public void paint(Graphics g){
    int first = 571;
    for (int i = 0; i < 6; i++){//Vertical lines
        g.drawLine(first, 75, first, 600);
        first += 71;
    }




    //day name positions
    sun.setBounds(510,75,40,10);
    mon.setBounds(580,75,40,10);
    tue.setBounds(650,75,40,10);
    wed.setBounds(710,75,40,10);
    thu.setBounds(790,75,40,10);
    fri.setBounds(860,75,40,10);
    sat.setBounds(930,75,40,10);

    //day number positions
    int second;





    first = 100;//horizontal line start and finish position 
    for (int i = 0; i < 7; i++){//horizontal lines
        if (i == 0)//indented on first line
        g.drawLine(525, first, 975, first);
        else 
            g.drawLine(500, first, 1000, first);

        first += 100;
    }

    for(int i = 0; i < 37; i++){//adding labels event marker
        add(eventMarker[i]);
    }



    first = 510;//position for event marker
    second = 125;//diddo
    for(int i = 0; i < 37; i++){//set position event marker
        for(int j = 0; j < 7; j++){//inner loop for a per week
            if((i+j) >= 37)//if index finishes, but loop still has days in week that would call out of bounds
                continue;
            eventMarker[i+j].setBounds(first,second,10,10);//added index's for specific day of week
            first+=71;

        }
        i+= 6;
        first = 510;
        second += 100;
    }

    last.setBounds(505,31,88,30);
    next.setBounds(895,31,88,30);
    month.setBounds(730,31,100,30);
    showYear.setBounds(800,31,100,30);
    addApt.addActionListener(this);
    //frame
    g.drawRect(0, 0, 500, 700);
    g.drawRect(500, 0, 500, 700);

    //text fields and labels bounds
    first = 120;
    second = 150;
    for(int i = 0; i < 5; i++){
        overFields[i].setBounds(first,second,100,20);
        second += 30;
        fields[i].setBounds(first, second, 300, 20);
        second += 50;
    }
    addApt.setBounds(300,600,100,30);

    repaint();

}//end of paint

public void repaint(){


    int first = 550;//date number position
    int second = 105;
    for(int i = 0; i < 37; i++){//Setting date number position
        for(int j = 0; j < 7; j++){//inner loop for a per week
            if((i+j) >= 37)//if index finishes, but loop still has days in week that would call out of bounds
                continue;
            allDays[i+j].setBounds(first,second,10,10);//added index's for specific day of week
            first+=71;

        }
        i+=6;
        //setting bounds
        second+=100;
        first=550;
    }
}

public void actionPerformed(ActionEvent evt){
    if (evt.getSource()==next){
        current++;
        if(current == 12){//resets to January
            current = 0;
            year++;
            String now="Year " + year;
            showYear.setText(now);


        }
        month.setText(months[current]);
        for (int i = 0; i < 37; i++){//setting proper day numbers
            if(allDays[i].getText().equals(monthDays[current==0 ? 11:current-1]))//so you don't access index -1
                dateHold=i +1;//for zero index of i
            dateHold%=7;//date left off at
        }

        //dateHold++;
        for(int j = 0; j < 37; j++)//erase dates
            allDays[j].setText("");

        for(int i = 0; i < 37; i++){//sets dates

            stringParse = Integer.parseInt(monthDays[current]);
            if(current==1 && (i+2) ==  stringParse && year%4!=0){//on the 28 of non leap years feb, print then break loop
                allDays[dateHold].setText("" + (i+1));
                break;
            }
            if((i+1) > stringParse)//without going over max per month
                continue;
            allDays[dateHold].setText("" + (i+1));
            dateHold++;
        }
        for(int i = 0; i < 37; i++){
            eventMarker[i].setText("|;");
        }

    }//end next evt

    if (evt.getSource()==last){//NOT CURRENTLY WORKING...
        current--;
        if(current == -1){//resets to February
            current = 11;
            year--;
            String now = "Year " +year;
            showYear.setText(now);
        }
        month.setText(months[current]);

        stringParse = Integer.parseInt(monthDays[current]);
        for (int i = 0; i < 7; i++){//check first week
            if(allDays[i].getText().equals("1")){
                dateHold =i;//plus then minus to fix index and next month
            }
        }
        for(int j = 0; j < 37; j++)//erase dates
            allDays[j].setText("");

        int something;//local number holder

        int countDown = stringParse;//local count down for reverse printing
        if(current ==1 && year%4!=0)//February fix
            countDown = 28;
        something = countDown - 1 + (dateHold);
        for (int i = 36; i >0; i--){//reverse date printing


            if(((36 -i)-dateHold)>stringParse)//if date over max
                continue;


            if(something < 0)
                continue;

            try{//kept reaching out of index here
            allDays[something].setText("" + (countDown));
            //System.out.println("i:" + i + "\ndateHold:" + dateHold + "\ncountDown:" + countDown);
            }
            catch(ArrayIndexOutOfBoundsException e){
                System.out.println("error i:" + i + "\ndateHold:" + dateHold + "\ncountDown:" + countDown);
            }
            countDown--;
            something--;
            if(countDown <= 0)
                break;
        }
        for(int i = 0; i < 37; i++){
            eventMarker[i].setText("|;");
        }

    }//end last evt

    String theDate;
    int numDate;
    if(evt.getSource() == fields[0]){
        theDate = fields[0].getText();

        try{
            numDate = Integer.parseInt(theDate);
            usrMonth[overallIndex] = months[(numDate-1)];
        }catch(NumberFormatException e){
            System.out.println("Error field[0]");
        }
        catch(ArrayIndexOutOfBoundsException e){}

    }//end of fields[0]

    if(evt.getSource() == fields[1]){
        theDate = fields[1].getText();
        try{
            numDate = Integer.parseInt(theDate);
            usrDay[overallIndex] = theDate;
        }catch(NumberFormatException e){System.out.println("Error field[1]");}
        catch(ArrayIndexOutOfBoundsException e){}
    }//end of fields[1]

    if(evt.getSource() == fields[2]){
        theDate = fields[2].getText();
        try{
            numDate = Integer.parseInt(theDate);
            usrYear[overallIndex] = numDate;
        }catch(NumberFormatException e){System.out.println("Error field[2]");}
        catch(ArrayIndexOutOfBoundsException e){}
    }//end of fields[2]

    if(evt.getSource() == fields[3]){
        theDate = fields[3].getText();
        usrInfo[overallIndex] = theDate;
    }

    if(evt.getSource() == addApt){
        if(usrDay[overallIndex] != null && usrMonth[overallIndex] != null){



            theDate = fields[1].getText();
            try{
                numDate = Integer.parseInt(theDate);
                usrDay[overallIndex] = theDate;
            }catch(NumberFormatException e){System.out.println("Error field[1]");}
            catch(ArrayIndexOutOfBoundsException e){}
            theDate = fields[2].getText();
            try{
                numDate = Integer.parseInt(theDate);
                usrYear[overallIndex] = numDate;
            }catch(NumberFormatException e){System.out.println("Error field[2]");}
            catch(ArrayIndexOutOfBoundsException e){}
            theDate = fields[3].getText();
            usrInfo[overallIndex] = theDate;




            System.out.println("Appointment Added\n" + (String) usrDay[overallIndex] +  "\n" + usrMonth[overallIndex] + "\n" + usrInfo[overallIndex] + "\n" +usrYear[overallIndex]);

            checkEvent();
            repaint();
            overallIndex++;//ONLY PLACE THIS CAN HAPPEN
            for (int i = 0; i < 5; i++)
                fields[i].setText("Dont forget to press Enter.");
        }
    }

}//end of action performed

public void checkEvent(){
    try{for(int i = 0; i < MAX; i++){//checking for events
        if(usrMonth[i].equals(months[current]) &&
                usrYear[i] == year){
            for(int j = 0; j < 37; j++){
                if(allDays[j].getText().equals(usrDay[i])){
                    String hold = eventMarker[j].getText();
                    hold += "*";
                    eventMarker[j].setText(hold);

                }
            }
        }
    }}
    catch(NullPointerException e){System.out.println("Null really");}
}


}
import java.awt.*;
导入java.applet.*;
导入java.awt.event.*;
公共类日历扩展小程序实现ActionListener{
/**
* 
*/
私有静态最终长serialVersionUID=1L;
私人按钮下一个,最后一个;
自有品牌月、展会年、太阳、周一、周二、周三、周四、周五、周六;
私有标签[]所有天=新标签[37];
私有标签[]eventMarker=新标签[37];
私有字符串[]月={“一月”、“二月”、“三月”、“四月”、“五月”、“六月”,
“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”};
私有字符串[]monthDays={“31”、“29”、“31”、“30”、“31”、“31”、“31”、“31”、“30”、“31”、“30”、“30”、“30”、“31”、“31”};
私有整数当前、年份、日期保持、字符串解析;
private final int MAX=1000;//事件数
//用于从文件输入数据的数组
私有int-overallIndex=0;
私有字符串[]usrMonth=新字符串[MAX];
私有字符串[]usrDay=新字符串[MAX];
私有int[]usrYear=新int[MAX];
//私有int[]usrTime=新int[MAX];
私有字符串[]usrInfo=新字符串[MAX];
私有文本字段[]字段=新文本字段[5];
私有标签[]overFields=新标签[5];
专用按钮addApt;
公共void init(){
设置大小(1050750);
年份=2012年;
当前=0;//数组中的月份
下一步=新按钮(“下一步”);
最后一个=新按钮(“最后一个”);
月份=新标签(月份[当前]);
showYear=新标签(“2012年”);
//日历上的日期名称
sun=新标签(“星期日”);
周一=新标签(“周一”);
星期二=新标签(“星期二”);
wed=新标签(“星期三”);
thu=新标签(“星期四”);
fri=新标签(“星期五”);
sat=新标签(“星期六”);
//日历上最初用于2012年1月的天数
字符串数;
对于(int i=0;i<37;i++){
如果(i>30)
allDays[i]=新标签(“”);
否则{
num=”“+(i+1);
allDays[i]=新标签(num);}
}
对于(int i=0;i<37;i++){
eventMarker[i]=新标签(“|::”);
}
addApt=新按钮(“添加约会”);
overFields[0]=新标签(“按编号输入月份”);
overFields[1]=新标签(“输入编号日期”);
overFields[2]=新标签(“输入年份”);
overFields[3]=新标签(“输入约会的任何信息”);
overFields[4]=新标签(“在此处搜索关键字”);
对于(int i=0;i<5;i++){
字段[i]=新文本字段(“别忘了按回车键。”,300);
}
//GUI添加
//钮扣
添加(下一个);
添加(最后一个);
add(addApt);
//标签
加(月);
添加(展示年);
加上(太阳);
增加(星期一);
加上(星期二);
加(星期三);
加(thu);
加上(星期五);
添加(sat);
//文本字段和actionListener
对于(int i=0;i<5;i++){
添加(overFields[i]);
添加(字段[i]);
字段[i].addActionListener(此);
}
for(int i=0;i=37)//如果索引完成,但循环在一周中仍有几天会调用越界
持续
eventMarker[i+j].setBounds(第一、第二、10、10);//添加了一周中特定日期的索引
第一+=71;
}
i+=6;
第一个=510;
秒+=100;
}
最后.挫折(505,31,88,30);;
其次是挫折(895,31,88,30);
月.挫折数(730,31100,30);
展示年.挫折数(800,31100,30);
addApt.addActionListener(这个);
//框架
g、 drawRect(0,0500700);
g、 drawRect(500,0500700);
//文本字段和标签边界
第一个=120;
秒=150;
对于(int i=0;i<5;i++){
超域[i].退边界(第一,第二,100,20);
秒+=30;
字段[i]。设置边界(第一、第二、300、20);
秒+=50;
}
附加立根(300600100,30);
重新油漆();
}//漆末
公共无效重绘(){
int first=550;//日期编号位置
int秒=105;
对于(int i=0;i<37;i++){//设置日期编号位置
对于(int j=0;j<7;j++){//内循环,每周一次
if((i+j)>=37)//如果索引完成,但循环在一周中仍有天会调用越界
持续
allDays[i+j].setBounds(第一、第二、10、10);//添加了一周中特定日期的索引
第一+=71;
}
i+=6;
//设定界限
秒+=100;
第一个=550;
}
}
已执行的公共无效操作(操作事件evt){
if(evt.getSource()==next){
电流++;
如果(当前==12){//重置为1月
电流=0;
年份++;
字符串now=“Year”+Year;
showYear.setText(现在);
}
month.setText(月份[当前]);
对于(int i=0;i<37;i++){//设置正确的天数
如果(allDays[i].getText().equals(monthDays[current==0?11:current-1])/,那么您不访问索引-1
dateHold=i+1;//对于i的零索引
dateHold%=7;//截止日期为
}
//dateHold++;
对于(int j=0;j<37;j++)//擦除日期
allDays[j].setText(“”);
对于(int i=0;i<37;i++){//设置日期
stringParse=Integer.parseInt(monthDays[current]);
如果(当前==1&&(i+2)==stringParse&&year%4!=0){//在非闰年2月28日,则打印,然后断开循环
allDays[dateHold].setText(“+(i+1));
打破
}
if((i+1)>stringParse)//不超过每月最大值
持续
allDays[dateHold].setText(“+(i+1));
dateHold++;
}
对于(int i=0;i<37;i++){
eventMarker[i].setText(“|”);
}
}//结束下一个evt
如果(evt.getSource()==la
usrMonth[i]
allDays[j]
allDays[j].getText()
eventMarker[j]