2011-11-18 70 views
-1

我已經做了一些關於這些類型的錯誤的搜索,仍然不能完全弄清楚我做錯了什麼。我的Applet不會啓動/初始化。 applet窗口彈出,然後說Start:applet未在底部初始化。Java Applet初始化Java.lang.NullpointException錯誤

下面是錯誤:

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); 
    } 

    } 


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


    } 
+0

666線。這很重要嗎?爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。請注意,不受信任的applet無法獲得File文件列表,並且Applet無法報告客戶端上僅限於服務器的目錄中的目錄的文件列表。 –

回答

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

路徑可以返回null。

嘗試聲明它,然後在init()中初始化它。

如果它爲null,則quoteFiles將爲空,因爲listFiles()很可能有一個catch,所以path將爲null。

告訴我,如果這有助於:d

Firexranger8的代碼

+0

我有點想知道如果路徑可能會返回null。我正在使用netbeans,它正在爲我的quotes文件夾添加我的projectname.quotes。我嘗試將name.quotes \\替換爲路徑中的引號\\,但我仍然得到相同的錯誤。 你說要嘗試聲明它並在init()中初始化它,所以基本上將我的聲明移動到我的方法中而不是它現在的位置,對吧? – Erin

+0

在嘗試readLines(String)之前,嘗試爲文件添加空檢查;方法。另外,爲了方便起見,我複製它並將它放在IDE中以更快地找到該行,並且行170顯然是: 'TimeDelay = lines [0] .length()* TimeWait;' – Matt

+0

我沒有複製幾條評論如果你正在尋找一個IDE,就不要在行號之前。 :)錯誤在readLines行。我將嘗試爲該方法寫一個空檢查。謝謝! – Erin