Java Applet初始化时出现Java.lang.NullpointException错误

时间:2011-11-18 19:05:05

标签: applet nullpointerexception panel java.lang.class

我已经对这些类型的错误进行了一些搜索,但仍然无法弄清楚我做错了什么。我的小程序无法启动/初始化。弹出小程序窗口,然后说开始:小程序未在底部初始化。

以下是错误:

java.lang.NullPointerException
at randomquotegenerator.quote.init(quote.java:170)
at sun.applet.AppletPanel.run(AppletPanel.java:424)
at java.lang.Thread.run(Thread.java:619)

作为quote.java:170错误返回的行在init()部分中:

readLines(quoteFiles[0].getName());

我有7个带引号的文本文件,每行一个,第一行是引号的标题。每行都用双引号“”包围。

  //****************************************************************************
  //****************************************************************************
  //** Imports                                                                **
  //****************************************************************************
  package randomquotegenerator;

  import java.io.*;
  import java.lang.*;
  import java.awt.*;
  import java.awt.event.*;
  import java.applet.*;
  import javax.swing.*;


  //****************************************************************************
  //** Class Definination                                                     **
  //****************************************************************************


  public class quote extends JApplet implements ActionListener,Runnable
  {

  //****************************************************************************
  //**  Class Global Variables                                               **
  //****************************************************************************

      int randomLine=0,maxLines=0,maxFiles=0,quoteLine=0,quoteFile=0;
      int doneFiles[] =new int[100];
      int doneLines[] =new int[200];
      int randomSwitch=-1,linearSwitch=-1;
      int TimeDelay=0,TimeWait=1;
      int currentFile=0,currentLine=0,unreadLines=0,unreadFiles=0;
      //int randomFile,randomLine;
      File path=new File("quotes\\");
      File[] quoteFiles=path.listFiles();

      Thread thread;
      Panel northPanel=new Panel();
      Panel centerPanel=new Panel();
      Panel southPanel=new Panel(new GridLayout(1,0));

      Panel Labels     = new Panel(new GridLayout(0,1));
      Panel ListBox1  = new Panel(new GridLayout(1,0));
      Panel ListBox2  = new Panel(new GridLayout(1,0));
      Panel ButtonSet1 = new Panel(new GridLayout(3,2));
      Panel ButtonSet2 = new Panel(new GridLayout(0,1));


      JLabel lab1=new JLabel("Quote Files");
      JLabel lab2=new JLabel("Current File");
      JLabel lab3=new JLabel("Quote Lines ");
      JLabel lab4=new JLabel("Current Line");
      JLabel lab5=new JLabel("TimeDelay ");
      JLabel lab6=new JLabel("    ");
      JLabel lab7=new JLabel("    ");

      List FilesList=new List(7);
      List LinesList=new List(7);

      JTextArea quoteDisplay= new JTextArea(18,36);
      JLabel quoteTitle = new JLabel("Java Applet Random Quote Displayer");


      Button PrevLine=new Button("Prev Line");
      Button NextLine=new Button("Next Line");
      Button PrevFile=new Button("Prev File");
      Button NextFile=new Button("Next File");
      Button ResetLine=new Button("Reset  Lines");
      Button ResetFile=new Button("Reset  Files");
      Button Settings=new Button("Settings ");
      Button NullButton1=new Button("         ");

      Button LinearButton=new Button("Linear Mode");
      Button RandomButton=new Button("Random Mode");
      Button ExitButton=new Button("Exit");

      boolean done=false;

      //String s=new String;;
      String lines[]=new String[200];



  //****************************************************************************
  //** Inititialize                                                                        
  //****************************************************************************

      @Override

      public void init(){

          setSize(768, 500);
          setLayout(new BorderLayout());

          add("North",northPanel);
          northPanel.setBackground(Color.blue);

          add("Center",centerPanel);
          centerPanel.setBackground(Color.black);

          add("South",southPanel);
          southPanel.setBackground(new Color(64,64,64));
          northPanel.setBackground(new Color(64,64,64));

          Labels.add(lab1);
          Labels.add(lab2);
          Labels.add(lab3);
          Labels.add(lab4);
          Labels.add(lab5);
          Labels.add(lab6);
          Labels.add(lab7);

          ListBox1.add(FilesList);
          ListBox2.add(LinesList);

          ButtonSet1.add(PrevLine);
          ButtonSet1.add(NextLine);
          ButtonSet1.add(PrevFile);
          ButtonSet1.add(NextFile);
          ButtonSet1.add(ResetLine);
          ButtonSet1.add(ResetFile);
          //ButtonSet1.add(Settings);
          //ButtonSet1.add(NullButton1);

          ButtonSet2.add(LinearButton);
          ButtonSet2.add(RandomButton);
          ButtonSet2.add(ExitButton);


          southPanel.add(Labels);
          southPanel.add(ListBox1);
          southPanel.add(ListBox2);
          southPanel.add(ButtonSet1);
          southPanel.add(ButtonSet2);
          centerPanel.add(quoteDisplay);
          northPanel.add(quoteTitle);
          //quoteDisplay.setColor(new Color(255,255,255));

          FilesList.addActionListener(this);
          LinesList.addActionListener(this);

          PrevLine.addActionListener(this);
          NextLine.addActionListener(this);
          PrevFile.addActionListener(this);
          NextFile.addActionListener(this);
          ResetLine.addActionListener(this);
          ResetFile.addActionListener(this);
          Settings.addActionListener(this);
          //NullButton1.addActionListener(this);
          LinearButton.addActionListener(this);
          RandomButton.addActionListener(this);
          ExitButton.addActionListener(this);

          Font font=new Font("times",Font.BOLD,24);
          quoteDisplay.setFont(font);
          quoteDisplay.setEditable(false);
          quoteDisplay.setLineWrap(true);
          quoteDisplay.setWrapStyleWord(true);

          Font font2=new Font("times",Font.BOLD,16);
          quoteTitle.setFont(font2);


          listFiles();
          readLines(quoteFiles[0].getName());


          FilesList.select(0);
          LinesList.select(0);
          TimeDelay=lines[0].length()*TimeWait;
          unreadFiles=maxFiles;
          unreadLines=maxLines;

          currentFile=0;
          currentLine=0;
          done=false;
          //maxLines=10;
          creatRandomFiles();
          creatRandomLines();

          lab1.setText("quote Files  =  "+maxFiles);
          lab2.setText("File = "+quoteFiles[0].getName());
          lab3.setText("quote Lines = "+maxLines);
          lab4.setText("Current Line = "+quoteLine);
          lab5.setText("TimeDelay  = "+TimeDelay);
          lab6.setText("Lines Unread "+unreadLines);
          lab7.setText("Files Unread "+unreadFiles);
          JOptionPane.showMessageDialog(null, " By Erin");

      }

  //*********************************************
  //*                                           *
  //*********************************************

  public void creatRandomFiles()
  {
    for (int i=0;i<maxFiles;i++)
    {
        doneFiles[i]=(int)(Math.random()*maxFiles);
        for(int j=0;j<i;j++) if (doneFiles[j]==doneFiles[i]) i--;
        //System.out.println("maxLines = "+doneLines[i]+"");
    }

  }

  //*********************************************
  //*                                           *
  //*********************************************

  public void creatRandomLines()
  {
    for (int i=0;i<maxLines;i++)
    {
        doneLines[i]=(int)(Math.random()*maxLines);
        for(int j=0;j<i;j++) if (doneLines[j]==doneLines[i]) i--;
        //System.out.println("maxLines = "+doneLines[i]+"");
    }

  }

  //*********************************************
  //*                                           *
  //*********************************************

  public void runLinearLines()
  {
           while(currentLine<maxLines)
            {
                    quoteLine=currentLine;
                    selectLine();
                    TimeDelay=lines[quoteLine].length()*TimeWait;
                    unreadLines=maxLines-currentLine;
                    lab5.setText("TimeDelay  = "+TimeDelay);
                    lab6.setText("Lines Unread "+unreadLines);
                    currentLine++;
                    System.out.println("Line Data:  "+ (currentLine+1)+ ". Line= "+doneLines[currentLine]+ ", Unread= "+ unreadLines+" ,time=" +TimeDelay+" ms");
                    try   {thread.sleep(TimeDelay);}
                    catch (Exception exception){}
             }

              currentLine=0;
              quoteLine=0;
              unreadLines=maxLines-currentLine;
              currentFile++;
              unreadFiles=maxFiles-currentFile;
              quoteFile=currentFile;
              FilesList.select(quoteFile);
            //FilesList.makeVisible(quoteFile);
              readLines(quoteFiles[FilesList.getSelectedIndex()].getName());
              lab6.setText("Lines Unread "+unreadLines);
              lab2.setText("File = "+quoteFiles[FilesList.getSelectedIndex()].getName());
              lab7.setText("Files Unread "+unreadFiles);

              System.out.println("-------------------------------------------------------------");
              System.out.println("File Data:  "+ (currentFile+1)+ ", " + quoteFiles[FilesList.getSelectedIndex()].getName()+". File= "+doneFiles[currentFile]+" ,time=" +TimeDelay+" ms");
              System.out.println("-------------------------------------------------------------");

              if (unreadFiles<0)
              {
                  currentFile=0;
                  currentLine=0;
              }

  }


  //*********************************************
 //*                                           *
  //*********************************************

  public void runRandomLines()
  {
           //currentLine=0;
           while(currentLine<maxLines)
           {
                    quoteLine=doneLines[currentLine];
                    selectLine();
                    TimeDelay=lines[quoteLine].length()*TimeWait;
                    unreadLines=maxLines-currentLine;
                    lab5.setText("TimeDelay  = "+TimeDelay);
                    lab6.setText("Lines Unread "+unreadLines);
                    currentLine++;
                    System.out.println("Line Data:  "+ (currentLine+1)+ ". Line= "+doneLines[currentLine]+ ", Unread= "+ unreadLines+" ,time=" +TimeDelay+" ms");
                    try   {thread.sleep(TimeDelay);}catch (Exception exception){}
             }

              currentLine=0;
              quoteLine=0;
              unreadLines=maxLines-currentLine;
              currentFile++;
              unreadFiles=maxFiles-currentFile;
              quoteFile=doneFiles[currentFile];
              FilesList.select(quoteFile);
            //FilesList.makeVisible(quoteFile);
              for (int i=0;i<200;i++) doneLines[i]=0;
              creatRandomLines();
              readLines(quoteFiles[FilesList.getSelectedIndex()].getName());
              lab6.setText("Lines Unread "+unreadLines);
              lab2.setText("File = "+quoteFiles[FilesList.getSelectedIndex()].getName());
              lab7.setText("Files Unread "+(unreadFiles+1));

              System.out.println("-------------------------------------------------------------");
              System.out.println("File Data:  "+ (currentFile+1)+ ", " + quoteFiles[FilesList.getSelectedIndex()].getName()+". File= "+doneFiles[currentFile]+" ,time=" +TimeDelay+" ms");
              System.out.println("-------------------------------------------------------------");

              if (unreadFiles<0)
              {
                  for (int i=0;i<100;i++) doneFiles[i]=0;
                  for (int i=0;i<200;i++) doneLines[i]=0;
                  creatRandomFiles();
                  creatRandomLines();
                  currentFile=0;
                  currentLine=0;
              }

            //System.out.println("------------------------------");

  }

  //****************************************************************************
  //**  Paint                                                                 **
  //****************************************************************************
  @Override
  public void paint(Graphics g)
  {


  }


  //****************************************************************************
  //**  Thread                                                                **
  //****************************************************************************

    @Override
    public void start()
   {
      thread = new Thread(this);
      thread.start();
    }
    @Override
    synchronized public void run()
    {
       while (true)
        {

         //*********************************************
          //*                                           *
          //*********************************************
          if (randomSwitch==1)
          {
              creatRandomLines();
              runRandomLines();

          }

          if (linearSwitch==1)
          {
            runLinearLines();
          }

       }
    }
   @Override
    public void stop()
    {
     if (thread != null) thread.stop();
    }


  //****************************************************************************
  //** actionPerformed                                                        **
  //****************************************************************************

    public void selectFile()
    {
        for (int i=0;i<200;i++) doneLines[i]=0;
         FilesList.select(quoteFile);
         //FilesList.makeVisible(quoteFile);
         readLines(quoteFiles[FilesList.getSelectedIndex()].getName());
         lab2.setText("File = "+quoteFiles[FilesList.getSelectedIndex()].getName());

    }

  //*********************************************
  //*                                           *
  //*********************************************

    public void selectLine()
    {

         LinesList.select(quoteLine);
         //LinesList.makeVisible(quoteLine);
         quoteDisplay.setText(lines[LinesList.getSelectedIndex()]);
         lab4.setText("Current Line = "+(quoteLine+1));
         TimeDelay=lines[quoteLine].length()*TimeWait;
         lab5.setText("TimeDelay  = "+TimeDelay);

    }
  //*********************************************
  //*                                           *
  //*********************************************

    @Override
    public boolean keyDown(Event event, int key)
    {
         switch(key)
         {
             case 10:
                //enter
             break;
             case 32:
                         //space
             break;

             case 27:
                    //esc
                    System.exit(0);
             break;


             case 1006:
                        //left
                     done=true;
                     quoteLine=0;
                     quoteFile--;if (quoteFile<1) quoteFile=0;
                     selectFile();
                     selectLine();

             break;

             case 1007:
                        //right
                     done=true;
                     quoteLine=0;
                     quoteFile++;if (quoteLine>maxFiles) quoteFile=maxFiles;
                     selectFile();
                     selectLine();

             break;

             case 1004:
                          //up
                     quoteLine--;if (quoteLine<1) quoteLine=0;
                     selectLine();
             break;

             case 1005:
                         //down
                     quoteLine++;if (quoteLine>maxLines) quoteLine=maxLines;
                     selectLine();

             break;


          }

           showStatus("(key= "+key+") ");

           return false;
    }

  //*********************************************
  //*                                           *
  //*********************************************
    @Override
    public void actionPerformed(ActionEvent event)
    {

     if (event.getSource()==ExitButton)
     {
         //System.exit(0);
     }

     if (event.getSource()==LinesList)
     {

         quoteLine=LinesList.getSelectedIndex();
         showStatus(FilesList.getSelectedItem()+" " +LinesList.getSelectedItem()+ "  "+ LinesList.getSelectedIndex());
        quoteDisplay.setText(lines[LinesList.getSelectedIndex()]);
        lab4.setText("Current Line = "+(quoteLine+1));
        //quoteTitle.setText(lines[LinesList.getSelectedIndex()]);
     }

     if (event.getSource()==FilesList)
     {
         quoteFile=FilesList.getSelectedIndex();
         lab2.setText("File = "+quoteFiles[FilesList.getSelectedIndex()].getName());
         showStatus(FilesList.getSelectedItem()+" " +LinesList.getSelectedItem()+ "  "+ LinesList.getSelectedIndex());
         readLines(quoteFiles[FilesList.getSelectedIndex()].getName());
         quoteLine=0;
         //selectFile();
         selectLine();
     }

     if (event.getSource()==PrevLine)
     {

         quoteLine--;if (quoteLine<1) quoteLine=0;
         selectLine();
     }

     if (event.getSource()==NextLine)
     {

         quoteLine++;if (quoteLine>maxLines) quoteLine=maxLines;
         selectLine();
     }

     if (event.getSource()==PrevFile)
     {
         done=true;
         quoteLine=0;
         quoteFile--;if (quoteFile<1) quoteFile=0;
         selectFile();
         selectLine();
     }

     if (event.getSource()==NextFile)
     {
         done=true;
         quoteLine=0;
         quoteFile++;if (quoteLine>maxFiles) quoteFile=maxFiles;
         selectFile();
         selectLine();
     }

     if (event.getSource()==ResetLine)
     {
         quoteLine=0;
         quoteFile=0;
         selectFile();
         selectLine();

     }

     if (event.getSource()==ResetFile)
     {
         quoteLine=0;
         quoteFile=0;
         selectFile();
         selectLine();
     }

     if (event.getSource()==LinearButton)
     {
       linearSwitch=-linearSwitch;


       if (linearSwitch==1)
       {
           randomSwitch=-1;
           LinearButton.setLabel("Linear Off ");
           RandomButton.setLabel("Random On");
       }
       else
       {
           LinearButton.setLabel("Linear On");
       }

       System.out.println("linearSwitch = " + linearSwitch+", randomSwitch= "+randomSwitch);
     }

     if (event.getSource()==RandomButton)
     {
       randomSwitch=-randomSwitch;


       if (randomSwitch==1)
       {
           linearSwitch =-1;
           RandomButton.setLabel("Random Off ");
           LinearButton.setLabel("Linear On");
       }
       else
       {
           RandomButton.setLabel("Random On");
       }

       System.out.println("linearSwitch = " + linearSwitch+", randomSwitch= "+randomSwitch);
     }

     if (event.getSource()==ExitButton)
     {
       System.exit(0);
     }

  }

  //*********************************************
  //*                                           *
  //*********************************************

  public void readLines(String fileName)
  {
        DataInputStream inStream;

        int count=0;
        LinesList.removeAll();

           try {
              inStream = new DataInputStream(
                            new FileInputStream("quotes\\"+fileName));

              while ( inStream.available() > 0 )
              {
                  lines[count]=inStream.readLine();
                  if (count==0)
                 {
                      quoteTitle.setText(lines[count]);
                      quoteDisplay.setText(lines[count]);
                  }
                  LinesList.add((count+1) + "");
                // System.out.println( lines[count]);
                 count++;
              } // End while.
             maxLines=count;
              lab3.setText("quote Lines = "+maxLines);

              inStream.close();
           } catch ( java.io.IOException exception)
           {
             if ( exception instanceof FileNotFoundException)
             {
                System.out.println(
                   "A file called test.txt could not be found, \n" +
                   "or could not be opened.\n"                     +
                   "Please investigate and try again.");
             }
           }
  }

  //*********************************************
  //*                                           *
  //*********************************************

  public void listFiles()
  {

       int i;
       maxFiles=0;

       if (quoteFiles !=null)
       {

       for (i=0;i<quoteFiles.length;i++)
       {
          // System.out.println(" "+i +"  " + quoteFiles[i]);
            FilesList.add(quoteFiles[i].getName());
            maxFiles++;


       }
       lab1.setText("quote Files  =  "+maxFiles);
   }

  }


  //****************************************************************************
  //**                                                                        **
 //****************************************************************************


  }

1 个答案:

答案 0 :(得分:0)

  File path=new File("quotes\\");
  File[] quoteFiles=path.listFiles();

路径可能会返回null。

尝试声明它,然后在init()中初始化它。

如果它为null,那么quoteFiles将为空,因为listFiles()很可能有一个catch,因此path将为null。

告诉我这是否有帮助:D

Firexranger8