Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我的程序中显示空错误;Stringtokenizer到数组_Java_Stringtokenizer - Fatal编程技术网

Java 为什么我的程序中显示空错误;Stringtokenizer到数组

Java 为什么我的程序中显示空错误;Stringtokenizer到数组,java,stringtokenizer,Java,Stringtokenizer,这本质上是我为实践而编写的一段小代码,它要求我使用StringTokenizer。我以前也做过类似的程序,但现在当我将字符串存储在数组中并尝试打印它们时,显示的是空指针异常。有什么帮助吗 import java.util.*; public class board1 { String key; String m[]; //function to accept the sentence void getsent() { Scanner in

这本质上是我为实践而编写的一段小代码,它要求我使用StringTokenizer。我以前也做过类似的程序,但现在当我将字符串存储在数组中并尝试打印它们时,显示的是空指针异常。有什么帮助吗

import java.util.*;
public class board1
{
    String key;
    String m[];

    //function to accept the sentence
    void getsent()
    {
        Scanner in=new Scanner(System.in);
        System.out.println("Enter a sentence terminated by'.' or '?'");
        String take=in.nextLine();
        StringTokenizer taken=new StringTokenizer(take);
        int numtokens=taken.countTokens();
        String m[]=new String[numtokens];
        for(int i=0;i<m.length;i++)
        {
            m[i]=taken.nextToken();
        }
        for(int i=0;i<m.length;i++)
        {
            System.out.print(m[i]);
        }
    }

    // function to display 
    void display()
    {
        System.out.println("The words seperately right now are:");
        for(int i=0;i<m.length;i++)
        {
            System.out.print(m[i]+"\t");
            System.out.println();
        }
    }

    // main to get functions
   public static void main(String args[])
   {
       board1 ob= new board1();
       ob.getsent();
       ob.display();  
   }
}
import java.util.*;
公共班级董事会1
{
字符串键;
字符串m[];
//接受句子的功能
void getsent()
{
扫描仪输入=新扫描仪(系统输入);
System.out.println(“输入以“.”或“?”结尾的句子”);
字符串take=in.nextLine();
StringTokenizer take=新的StringTokenizer(take);
int numtokens=take.countTokens();
字符串m[]=新字符串[numtokens];
对于(int i=0;i您是变量
m
。替换

String m[] = new String[numtokens];

您是变量
m
。替换

String m[] = new String[numtokens];


我认为这是因为您正在着色属性。您有一个名为m的数组,您在getSent中将令牌放入其中,但display使用的是在类中定义的m数组,您没有添加任何内容


在display中打印出m的大小,这将显示您没有向名为m的属性添加任何内容。

我认为这是因为您正在对属性进行着色处理。您有一个名为m的数组,您将令牌放入getSent中,但display使用的是在类中定义的m数组,您没有添加任何内容


在显示屏上打印出m的大小,这将显示您没有向名为m的属性添加任何内容。

非常感谢!这对我帮助很大,也将对我即将到来的期末考试有所帮助!非常感谢!这对我帮助很大,也将对我即将到来的期末考试有所帮助!谢谢:)你们是最好的!谢谢:)你们是最好的!