-2

我有一个程序,目前除了显示时间并允许用户按下一个按钮,该按钮将引导他进入另一个屏幕,我将显示一个对话框以显示成功运行(仅供参考,应用程序的名称是 SuperAlarms,我正在使用 Eclipse 插件)。该程序编译得非常好(尽管有一些错误表明某些变量没有被使用)但是一旦我加载程序我得到一个“java.lang.NullPointerException”。

超级警报.java:

/*
 * Description:
 *  A convenient alarm clock application that will play a song and provide a "pause" button
 *  to pause the music so it can be continued later
 * Author: Dennis Plotnik
 * Copyright: Standard Intellectual Property Copyright Laws Apply (2012)
 * Version: 1.0
 * Methods used:
 * - Date
 * - Threads
 * - Music Streams
 * - GUI
 *  
 */

package com.galaxy.dennis;

import net.rim.device.api.ui.UiApplication;

/**
 * This class extends the UiApplication class, providing a
 * graphical user interface.
 */
public class SuperAlarms extends UiApplication
{
    /**
     * Entry point for application
     * @param args Command line arguments (not used)
     */ 
    public static void main(String[] args)
    {
        // Create a new instance of the application and make the currently
        // running thread the application's event dispatch thread.
        SuperAlarms theApp = new SuperAlarms();       
        theApp.enterEventDispatcher();
    }


    /**
     * Creates a new SuperAlarms object
     */
    public SuperAlarms()
    {        
        // Push a screen onto the UI stack for rendering.
        pushScreen(new SuperAlarmsMainScreen());
    }    
}

SuperAlarmsMainScreen.java:

package com.galaxy.dennis;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.FontFamily;
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;

/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class SuperAlarmsMainScreen extends MainScreen implements FieldChangeListener
{
    private TimeUpdater updater;
    private HorizontalFieldManager hfm_centerTime;
    private Alarm[] alarms;
    private ButtonField bf_newAlarm;
    private GridOrganizer g;
    private FontFamily comicSansMs;
    private Font fnt_title;
    private Font fnt_main;


    //LabelFields
        private LabelField lf_curTime;

    public SuperAlarmsMainScreen()
    {
        //FieldManagers
            hfm_centerTime = new HorizontalFieldManager(Field.FIELD_HCENTER);
            g = new GridOrganizer(0, null);

        //Fonts:
        try {
            comicSansMs = FontFamily.forName("Comic Sans MS");
            fnt_title = comicSansMs.getFont(Font.BOLD, 16, Ui.UNITS_pt);
            fnt_main = comicSansMs.getFont(Font.PLAIN, 12, Ui.UNITS_pt);
        } catch(ClassNotFoundException e) { e.printStackTrace(); }

        //Title:
            setTitle("SuperAlarms");

        //Updater:
            for (int i=0;i<5;i++) alarms[i] = new Alarm(null, null, null, null);
            updater = new TimeUpdater(alarms);
            updater.start();


        //Labelfields:
            lf_curTime = new LabelField(updater.getTime());
            lf_curTime.setFont(fnt_title);
            hfm_centerTime.add(lf_curTime);

            add(hfm_centerTime);
            add(new SeparatorField());

        //Bottom
            bf_newAlarm = new ButtonField("Create New Alarm", ButtonField.CONSUME_CLICK);
            bf_newAlarm.setChangeListener(this);
    }

    public void fieldChanged(Field field, int context) {
        if (field == bf_newAlarm) {
            UiApplication.getUiApplication().pushScreen(new NewAlarmScreen());
        }

    }
}

NewAlarmScreen.java:

package com.galaxy.dennis;

import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.MainScreen;

public class NewAlarmScreen extends MainScreen {
    public NewAlarmScreen() {
        Dialog.alert("You clicked that button!");
    }
}

GridOrganizer.java:

package com.galaxy.dennis;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

public class GridOrganizer {

    private HorizontalFieldManager[] hfm;
    private VerticalFieldManager[] vfm;
    private int row; 
    private MainScreen screen;

    public GridOrganizer(int row, MainScreen mainscreen) {
        this.row = row;
        this.screen = mainscreen;
        hfm = new HorizontalFieldManager[row];
        for (int i=0;i<row;i++) hfm[i] = new HorizontalFieldManager(Field.USE_ALL_WIDTH);
        vfm = new VerticalFieldManager[row];
        for (int i=0;i<row;i++) vfm[i] = new VerticalFieldManager(Field.USE_ALL_WIDTH);
    }

    public void add(int row, int col, Field obj) {

        switch(col) {
        case 1:
            hfm[row-1].add(obj);
            break;
        case 2:
            vfm[row-1].add(obj);
        }
    }

    public void draw(boolean addLines) {
        for (int i=0;i<this.row;i++) {
            hfm[i].add(vfm[i]);
        }
        for (int i=0;i<this.row;i++) {
            screen.add(hfm[i]);
            if (addLines)
                screen.add(new SeparatorField());
        }

    }
}

TimeUpdater.java:

package com.galaxy.dennis;

import java.util.Date;

import net.rim.device.api.i18n.SimpleDateFormat;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;

public class TimeUpdater extends Thread {
    private String time = "0:00";
    private int[][] days;
    private Alarm[] alarm;

    public TimeUpdater(Alarm[] alarms) {
        this.alarm = alarms;
        Date date = new Date();
        SimpleDateFormat hours = new SimpleDateFormat("H");
        SimpleDateFormat minutes = new SimpleDateFormat("mm");
        String h = hours.format(date);
        String m = minutes.format(date);
        time = h + ":" + m;
    }

    public void run() {
        UiApplication.getUiApplication().invokeLater(new Runnable() {

            public void run() {
                Date date = new Date();
                SimpleDateFormat hours = new SimpleDateFormat("H");
                SimpleDateFormat minutes = new SimpleDateFormat("mm");
                String h = hours.format(date);
                String m = minutes.format(date);
                time = h + ":" + m;
                for (int i=0;i<5;i++) {
                    if (alarm[i].getTime() == time) Dialog.alert("Alarm: " + alarm[i].getName() + "!");
                }
            }
        }, time(30), true);
    }

    public String getTime() {
        return time;
    }

    public long time(int s) {
        return s*1000;
    }
}

Alarm.java:包com.galaxy.dennis;

public class Alarm {
    private String time;
    private String song;
    private String name;
    private int[] days;

public Alarm(String t, String s, String n, int[] d) {
    this.time = t;
    this.song = s;
    this.name = n;
    this.days = d;      
}

public void setTime(String str) {
    this.time = str;
}

public void setSong(String str) {
    this.song= str;
}

public void setName(String str) {
    this.name = str;
}

public void setDays(int[] d) {
    this.days = d;
}

public String getTime() {
    return this.time;
}

public String getSong() {
    return this.song;
}

public String getName() {
    return this.name;
}

public int[] getDays() {
    return this.days;
}
}

为什么我会收到此错误?

编辑:编译消息:

    Packaging project SuperAlarms B:\Apps\Eclipse\plugins\net.rim.ejde.componentpack6.0.0_6.0.0.43\components\bin\rapc.exe
    -convertpng -quiet codename=deliverables\Standard\6.0.0\SuperAlarms -sourceroot=B:\Programming\Java\BlackBerry\SuperAlarms\src;B:\Programming\Java\BlackBerry\SuperAlarms\res
    -import=B:\Apps\Eclipse\plugins\net.rim.ejde.componentpack6.0.0_6.0.0.43\components\lib\net_rim_api.jar deliverables\Standard\6.0.0\SuperAlarms.rapc B:\Programming\Java\BlackBerry\SuperAlarms\bin 
B:\Programming\Java\BlackBerry\SuperAlarms\src\com\galaxy\dennis\Alarm.java:9: Warning!: member data 'String song' not required in class: com.galaxy.dennis.Alarm 
B:\Programming\Java\BlackBerry\SuperAlarms\src\com\galaxy\dennis\Alarm.java:9: Warning!: member data 'int[] days' not required in class: com.galaxy.dennis.Alarm 
B:\Programming\Java\BlackBerry\SuperAlarms\src\com\galaxy\dennis\Alarm.java:17: Warning!: method 'com.galaxy.dennis.Alarm.setTime(String)' not invoked. 
B:\Programming\Java\BlackBerry\SuperAlarms\src\com\galaxy\dennis\Alarm.java:21: Warning!: method 'com.galaxy.dennis.Alarm.setSong(String)' not invoked. 
B:\Programming\Java\BlackBerry\SuperAlarms\src\com\galaxy\dennis\Alarm.java:25: Warning!: method 'com.galaxy.dennis.Alarm.setName(String)' not invoked. 
B:\Programming\Java\BlackBerry\SuperAlarms\src\com\galaxy\dennis\Alarm.java:29: Warning!: method 'com.galaxy.dennis.Alarm.setDays(int[])' not invoked. 
B:\Programming\Java\BlackBerry\SuperAlarms\src\com\galaxy\dennis\Alarm.java:37: Warning!: method 'com.galaxy.dennis.Alarm.getSong()' not invoked. 
B:\Programming\Java\BlackBerry\SuperAlarms\src\com\galaxy\dennis\Alarm.java:45: Warning!: method 'com.galaxy.dennis.Alarm.getDays()' not invoked. 
B:\Programming\Java\BlackBerry\SuperAlarms\src\com\galaxy\dennis\GridOrganizer.java:15: Warning!: member data 'int row' not required in class: com.galaxy.dennis.GridOrganizer
B:\Programming\Java\BlackBerry\SuperAlarms\src\com\galaxy\dennis\GridOrganizer.java:15: Warning!: member data 'MainScreen screen' not required in class: com.galaxy.dennis.GridOrganizer 
B:\Programming\Java\BlackBerry\SuperAlarms\src\com\galaxy\dennis\GridOrganizer.java:26: Warning!: method 'com.galaxy.dennis.GridOrganizer.add(int,int,Field)' not invoked. 
B:\Programming\Java\BlackBerry\SuperAlarms\src\com\galaxy\dennis\GridOrganizer.java:36: Warning!: method 'com.galaxy.dennis.GridOrganizer.draw(boolean)' not invoked. 
B:\Programming\Java\BlackBerry\SuperAlarms\src\com\galaxy\dennis\SuperAlarmsMainScreen.java:35: Warning!: member data 'Alarm[] alarms' not required in class: com.galaxy.dennis.SuperAlarmsMainScreen 
B:\Programming\Java\BlackBerry\SuperAlarms\src\com\galaxy\dennis\SuperAlarmsMainScreen.java:35: Warning!: member data 'GridOrganizer g' not required in class: com.galaxy.dennis.SuperAlarmsMainScreen 
B:\Programming\Java\BlackBerry\SuperAlarms\src\com\galaxy\dennis\SuperAlarmsMainScreen.java:35: Warning!: member data 'Font fnt_main' not required in class: com.galaxy.dennis.SuperAlarmsMainScreen 
Packaging project SuperAlarms succeeded (took 2.932 seconds)

这是我启动程序后发生的情况的图片: 在此处输入图像描述

4

1 回答 1

0

一个疯狂的猜测是FontFamily.forName("Comic Sans MS")返回 null。先尝试不设置字体。

于 2012-11-07T16:14:56.387 回答