NullPointException当我尝试使用按钮在j2me上导航窗体时

我无法通过点击一个按钮将主页链接到第二页。它抛出一个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();
        }
2
额外 编辑
意见: 1
尝试使用资源编辑器来做这件事。我是J2ME中的新编码,它会比这更容易。
额外 作者 Mun0n,
1)为了更快提供更好的帮助,请发布 SSCCE 。 2)请对代码块使用一致的逻辑缩进并将“空白”行限制为不超过1次。 3)+1给@mprabhat - 任何人都没有机会通过查看代码来解决问题。
额外 作者 Andrew Thompson,
点击哪个按钮会给你这个例外?你也可以发布stacktrace
额外 作者 mprabhat,
发布异常堆栈跟踪
额外 作者 gnat,

1 答案

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

4
额外