2

我无法通过单击按钮将主页链接到第二页。它抛出一个 NullPointException。有人可以告诉我我错在哪里了吗?

import java.io.InputStream;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.*;
import com.sun.lwuit.*;
import com.sun.lwuit.animations.CommonTransitions;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.util.Resources;
import java.io.IOException;

public class standings extends MIDlet implements ActionListener{

   public Command cmdSelect; 
   public Form fform, selectLgform;
   public Button btnSelectLeague,btnHelp,btnAbout,btnExit;
   public TextArea taHome,taAbout, taHelp, taSelectLg;
    private InputStream iStream;
    private StringBuffer strBuffer;
    private Form selectLgForm;
    public void startApp() {
        Display.init(this);
        try {
            Resources rs= Resources.open("/restheme.res");
            UIManager.getInstance().setThemeProps(rs.getTheme("Theme2"));
        } catch (Exception e) {
            e.getMessage();
        }
       displayMainForm();
    }

  public void displayMainForm()
  {
   fform = new Form("Football League Standings");
   taHome= new TextArea(5,20,TextArea.ANY);
   fform.setLayout(new BorderLayout());
   fform.setTransitionInAnimator(CommonTransitions.createFade(1000));
   iStream     = getClass().getResourceAsStream("/intro.txt");
   strBuffer   = new StringBuffer();
        int next    = 1;
        try {
            while((next = iStream.read()) != -1) {
                char nextChar = (char) next;
                strBuffer.append(nextChar);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        taHome.setText(strBuffer.toString());
        strBuffer   = null;
        taHome.setFocusable(false);
        taHome.setEditable(false);
        taHome.setUIID("Label");

        //fform.addComponent(BorderLayout.CENTER,taHome);

        btnSelectLeague= (new Button("Select league"));
        btnHelp= (new Button("Help"));
        btnAbout= (new Button("About"));
        btnExit= (new Button("Exit"));

    Container mcont = new Container(new BoxLayout(BoxLayout.Y_AXIS));
      mcont.addComponent(taHome);
      mcont.addComponent(btnSelectLeague);
      mcont.addComponent(btnHelp);
      mcont.addComponent(btnAbout);
      mcont.addComponent(btnExit);
      fform.addComponent(BorderLayout.CENTER, mcont);
      btnSelectLeague.addActionListener(this);
      btnHelp.addActionListener(this);
      btnAbout.addActionListener(this);
      btnExit.addActionListener(this);

   fform.show();
  }

  /* Command exitCommand = new Command("Exit");
   fform.addCommand(exitCommand);

   fform.addCommandListener(this);
    }
  }*/
   public void selectLgForm()
    {
        selectLgForm= new Form("Select League");
        taSelectLg= new TextArea(5,20,TextArea.ANY);
        selectLgForm.setLayout(new BorderLayout());
        selectLgForm.setTransitionInAnimator(CommonTransitions.createFade(1000));
        iStream     = getClass().getResourceAsStream("/selectlg.txt");
        strBuffer   = new StringBuffer();
        int next    = 1;
        try {
            while((next = iStream.read()) != -1) {
                char nextChar = (char) next;
                strBuffer.append(nextChar);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        taSelectLg.setText(strBuffer.toString());
        strBuffer   = null;
        taSelectLg.setFocusable(false);
        taSelectLg.setEditable(false);
        taSelectLg.setUIID("Label");

        selectLgForm.addComponent(BorderLayout.CENTER,taAbout);

    //START OF MENU BUTTONS
        selectLgForm.addCommand(new Command("Back")
        {
            public void actionPerformed(ActionEvent e)
            {
                displayMainForm();
            }


        });


        selectLgForm.show();
    }//END OF MENU BUTTONS


    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

  public void actionPerformed(ActionEvent ae) {
       if(ae.getSource()==btnSelectLeague){
            selectLgForm();
        }
4

1 回答 1

4

我最好的猜测是:
-selectLgForm.addComponent(BorderLayout.CENTER,taAbout)抛出 NullPointerException 因为taAbout为空。
- 或iStream = getClass().getResourceAsStream("/selectlg.txt");抛出 NullPointerException,因为文本文件不在您认为的位置。

于 2012-05-25T09:47:57.293 回答