2016-07-11 19 views
-2

私は、ランダムな段落で単語が何回使用されたのかを数えられるプログラムを作っていました。それはコンパイルされますが、私がそれを実行しようとすると、私にNullPointerExceptionを与えます。このNullPointerExceptionを処理するには?

は、ここでは、コードです:

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

ない重複で初期化されていません。これは制御フローの問題です。 – Will

+0

Btw Mohit、問題は 'c [i] .SetCount(1、test [i])'です。 'c'配列には' Count'インスタンスではなく 'null'要素が含まれています。それを修正してください。また、ここであなたの質問と一致するので、提案された複製を受け入れてください。 – Tom

+0

は、そう私はそれらのインスタンスを初期化するために忘れてしまった...私の悪い:Oそれは今実行しているようですが、私はの奇妙な出力取得しています : - outCount = 143がまだ同じ例外 –

答えて

1

主な問題は、あなたがCount[]の配列を作成したにも関わらず、である、あなたは実際には、アレイ内の各位置でCount()オブジェクトを初期化していません。

Count[] c = new Count[size]; 

これは、配列自体を初期化し、それはまだ、配列の各位置で初期化さCount()オブジェクトを配置しません。 、

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

あなたループst.hasMoreTokens()ながら、しかし、あなたはst.nextToken()size回を呼び出し続ける:

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

もう一つの問題は、ここのようです:あなたは、実際にこのようなnew Count()でこれらの新しいオブジェクトを作成して割り当てる必要がありますそして終わりに達する。

代わりにこれを試してください:あなたがc[i].SetCount(.....)を行うと

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

= 名前を'nextToken')!= NullPointerException'(OPによって報告されました)....または質問として書かれています:OPコードからのスニペットで' NullPointerException'がスローされ、どのようにコードが修正されるのですか? –

+2

'NoSuchElementException'が(でスロー – Tom

+0

ループ全体が意味をなさない。 'test [i]'も設定されません。 – Fildor

1

c[i]でオブジェクトがStringTokenizer`が対処されていない `との具体的な問題としてnew Count()

関連する問題