将arraylist方法更改为linkedlist方法

时间:2014-12-20 01:37:56

标签: java linked-list

我有一个arraylist方法,我需要更改为linkedlist方法。它在int x line处抛出一个nullpointerexception。它正在向另一行抛出另一个nullpointerexception。但该行是调用该方法的位置。以下是代码:

  Exception in thread "main" java.lang.NullPointerException
  at PlantList.analyzePlants(PlantList.java:303)
  at PlantList.main(PlantList.java:434)


 private void analyzePlants() {
          String text = "";

          Node p = root;
          while(p!=null) {
              text = JOptionPane.showInputDialog(null,"Enter the string to analyze: " );

           int x = analyze(p.plant.getName().toLowerCase(), analyzeText.getText().toLowerCase());
           text += p.plant.getName() + " : " + x + "\n";
           p =p.next;
          }
          JTextArea textArea = new JTextArea(text);
          JScrollPane scrollPane = new JScrollPane(textArea);
          textArea.setLineWrap(true);
          textArea.setWrapStyleWord(true);
          scrollPane.setPreferredSize(new Dimension(150, 300));
          JOptionPane.showMessageDialog(null, scrollPane, "Analyzation Completed",   JOptionPane.CANCEL_OPTION);
          analyzeText.setText("");
           p = p.next;
    }
    /**
     * Recursive function to analyze the plants in the system.
     */
    private int analyze(String name, String text) {
          if (!name.contains(text)) {
           return 0;
          }
          if (name.startsWith(text)) {
           return 1 + analyze(name.substring(1), text);
          } else {
           return 0 + analyze(name.substring(1), text);
          }
         }

1 个答案:

答案 0 :(得分:0)

  

它在int x line

抛出nullpointerexception

p.plantp.plant.getName()analyzeTextanalyzeText.getText()为空。

更新在调用分析文本的任何方法之前执行空检查。 E.g。

String param1 = p.plant.getName().toLowerCase()
String param2 = "";//you can also initialize to null
if(analyzeText != null && analyzeText.getText() != null) {param2 = analyzeText.getText().toLowerCase()}
int x = analyze(param1, param2);