2016-07-11 20 views
-2

Ich machte ein Programm, wo es zählen würde, wie oft jedes Wort in einem zufälligen Absatz verwendet wurde. Es kompiliert, aber wenn ich versuche, es auszuführen, gibt es mir eine NullPointerException.Wie mit dieser NullPointerException umgehen?

Hier ist der Code:

import java.util.StringTokenizer; 

class Count 
{ 
    int count; 
    String name; 

    void SetCount(int c, String n) 
    { 
     count = c; 
     name = n; 
    } 

    void Show() 
    { 
     System.out.print("Name=" + name); 
     System.out.print("Count=" + count); 
    } 
} 

class Contains2 extends Count 
{ 
    public static void main(String args[]) 
    { 
     String s = "Once you have made it to the box office and gotten your tickets, you are confronted with the problems of the theater itself. If you are in one of the run-down older theaters, you must adjust to the musty smell of seldom-cleaned carpets. Escaped springs lurk in the faded plush or cracked leather seats, and half the seats you sit in seem loose or tilted so that you sit at a strange angle. The newer twin and quad theaters offer their own problems. Sitting in an area only one-quarter the size of a regular theater, moviegoers often have to put up with the sound of the movie next door. This is especially jarring when the other movie involves racing cars or a karate war and you are trying to enjoy a quiet love story. And whether the theater is old or new, it will have floors that seem to be coated with rubber cement. By the end of a movie, shoes almost have to be pried off the floor because they have become sealed to a deadly compound of spilled soda, hardening bubble gum, and crushed Ju-Jubes"; 
     int size, i, count = 0, j; 

     size = s.length(); 
     String[] test = new String[size]; 

     Count[] c = new Count[size]; 

     StringTokenizer st = new StringTokenizer(s, " "); 

     while (st.hasMoreTokens()) 
     { 
      for (i=0; i < size; i++) 
      { 
       test[i] = st.nextToken(); 
       c[i].SetCount(1, test[i]); 
      } 
     } 

     for (i=0; i<size; i++) 
     { 
      for (j=0; j<size; j++) 
      { 
       if (c[i].name.equals(test[j])) 
        c[i].count+=1; 
      } 
     } 

     for (i=0; i<size; i++) 
     { 
      c[i].Show(); 
     } 
    } 
} 
+0

kein Duplikat als das spezifische Problem mit 'StringTokenizer' ist dort nicht angesprochen. Dies ist ein Kontrollflussproblem. – Will

+0

Btw Mohit, dein Problem ist diese Zeile 'c [i] .SetCount (1, test [i])', weil dein 'c' Array' null' Elemente enthält, nicht 'Count' Instanzen. Repariere das. Bitte akzeptieren Sie auch das vorgeschlagene Duplikat, da es Ihrer Frage hier entspricht. – Tom

+0

ja ich vergaß diese Instanzen ... mein schlecht zu initialisieren: O es nun zu laufen scheint, aber ich bin einen seltsamen Ausgang des Erhalten: - Namen = outCount = 143 –

Antwort

1

Das Hauptproblem ist, auch wenn Sie eine Reihe von Count[] erstellt haben, haben Sie nicht wirklich ein Count() Objekt an jeder Position in dem Array initialisiert.

Count[] c = new Count[size]; 

Dies initialisiert den Array selbst, aber es stellen immer noch keine initialisierten Count() Objekt an jeder Stelle des Arrays. Sie müssen tatsächlich erstellen und diese neuen Objekte mit new Count() wie folgt vergeben:

for (int i=0; i<size; i++) 
{ 
    c[i] = new Count(); 
} 

Ein weiteres Problem scheint hier zu sein:

while (st.hasMoreTokens()) 
{ 
    for (i=0; i<size; i++) 
    { 
     test[i] = st.nextToken(); 
     c[i].SetCount(1, test[i]); 
    } 
} 

Sie Schleife während st.hasMoreTokens(), aber dann halten Sie st.nextToken()size mal anrufen, und das Ende ist erreicht.

Versuchen Sie stattdessen:

import java.util.StringTokenizer; 


class Contains2 extends Count 
{ 
    public static void main(String args[]) 
    { 
     String s = "Once you have made it to the box office and gotten your tickets, you are confronted with the problems of the theater itself. If you are in one of the run-down older theaters, you must adjust to the musty smell of seldom-cleaned carpets. Escaped springs lurk in the faded plush or cracked leather seats, and half the seats you sit in seem loose or tilted so that you sit at a strange angle. The newer twin and quad theaters offer their own problems. Sitting in an area only one-quarter the size of a regular theater, moviegoers often have to put up with the sound of the movie next door. This is especially jarring when the other movie involves racing cars or a karate war and you are trying to enjoy a quiet love story. And whether the theater is old or new, it will have floors that seem to be coated with rubber cement. By the end of a movie, shoes almost have to be pried off the floor because they have become sealed to a deadly compound of spilled soda, hardening bubble gum, and crushed Ju-Jubes"; 
     int size, count = 0; 


     StringTokenizer st = new StringTokenizer(s, " "); 
     size = st.countTokens(); 

     Count[] c = new Count[size]; 
     String[] test = new String[size]; 

     while (st.hasMoreTokens()) 
     { 
      String token = st.nextToken(); 

      for (int i=0; i<size; i++) 
      { 
       test[i] = token; 

       c[i] = new Count(); 
       c[i].SetCount(1, token); 
      } 
     } 

     for (int i=0; i<size; i++) 
     { 
      for (int j=0; j<size; j++) 
      { 
       if (c[i].name.equals(test[j])) 
        c[i].count+=1; 
      } 
     } 

     for (int i=0; i<size; i++) 
     { 
      c[i].Show(); 
     } 
    } 
} 

public class Count 
{ 
    protected int count; 
    protected String name; 

    public void SetCount(int c, String n) 
    { 
     count = c; 
     name = n; 
    } 

    public void Show() 
    { 
     System.out.println("Name=" + name); 
     System.out.println("Count=" + count); 
    } 
} 
+0

noch gleiche Ausnahme –

+2

'NoSuchElementException' (geworfen von 'nextToken')! =' NullPointerException' (gemeldet von OP) .... oder als Frage geschrieben: Wie soll dieser Code-Ausschnitt aus dem OP-Code eine 'NullPointerException' auslösen und wie korrigiert das den Code? – Tom

+0

Die ganze Schleife macht nicht viel Sinn. 'test [i]' wird ebenfalls nicht gesetzt. – Fildor

1

Wenn Sie c[i].SetCount(.....) das Objekt in c[i] tun wurde nicht initialisiert mit new Count()

Verwandte Themen