我正在为大学项目创建一个WordSearch拼图,并且存在空值问题。我不知道如何调用我的驱动程序测试 [英] I am creating a WordSearch puzzle for a university project and having problems null values. I am not sure how to call my driver tests

查看:56
本文介绍了我正在为大学项目创建一个WordSearch拼图,并且存在空值问题。我不知道如何调用我的驱动程序测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用驱动程序从文本文件中获取单词以使用wordsearch拼图。我是新手使用驱动程序,并不知道如何使用它们。驱动程序不工作,当我运行我的代码作为一个整体时,我得到一个错误java.lang.NullPointerException。我已经在公共WordSearchPuzzle(String wordFile,int wordCount,int shortest,int longest)&在司机内。我还包括我的方法来从文件中获取单词。



  import  java.awt。*; 
import javax.swing。*;
import java.util。*;
import java.util.ArrayList;
import java.io. *;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;




public class WordSearchPuzzle
{
private char [] [] puzzle;
private ArrayList< String>拼图;



public WordSearchPuzzle( String wordFile, int wordCount,
int shortest, int longest)
{

puzzleWords = loadWords(wordFile,shortest,longest);
ArrayList< String> chosenWords = new ArrayList< String> ();
int i = 0;
int sum = 0;
int gridSize = 0;
for (i = 0; i< wordCount; i ++)
{
int positionOfWord =( int )((Math.random()* puzzleWords.size())); / / 在终端这一行返回时出现空错误
chosenWords.add(puzzleWords.get(positionOfWord));
}

for (i = 0; i< chosenWords.size(); i ++)
{
sum + = chosenWords.get(i).length();
}
gridSize = sum * 2 ;
gridSize =( int )(Math.sqrt(gridSize));
.puzzle = new char [gridSize] [gridSize];
puzzleWords = selectedWords;


}


private ArrayList< String> loadWords( String wordFile, int shortest, int 最长)
{
尝试
{
FileReader aFileReader = new FileReader(wordFile);
BufferedReader aBuffer = new BufferedReader(aFileReader);
String fileLine;
int lengthOfWord;
ArrayList< String> words = new ArrayList< String>();
fileLine = aBuffer.readLine();
while (fileLine!= null)
{
lengthOfWord = fileLine.length();
if (lengthOfWord> = shortest&& lengthOfWord< = longest)
{
words.add(fileLine);
}
fileLine = aBuffer.readLine();
}
aBuffer.close();
aFileReader.close();
返回字;
}
catch (IOException x)
{
return null;
}
}




DRIVER ................... .....................

import java.util。*;
import java.util.ArrayList;

public class 驱动程序
{
public static void main( String [] args)
{
// System .out.println();
// System.out.println(** ************************以下是对来自文件的随机生成单词的测试************** ****************** + | n);
System.out.println();
Driver.test1(); // 在终端此行回来时出现空错误



}
private static void test1()
{ // 此测试用例是来自文件bncwords.txt中的随机单词,要选择100个单词,最短长度= 3,最长长度= 3

WordSearchPuzzle wordSearch = new WordSearchPuzzle( BasicEnglishWords.txt 50 2 9 ); // 此行返回时出现空错误
wordSearch.getWo rdSearchList(); // 获取所选单词的列表
wordSearch.showWordSearchPuzzle();

System.out.println(); // 打印拼图
}

}

解决方案

  public   class 驱动程序
{
public Driver(){ // konstruktor
test1();
}


public static void main( String [] args)
{
// System.out.println();
// System.out.println(**************************以下是随机测试来自文件的生成词******************************** + | n);
System.out.println();
// Driver.test1(); //在终端中,此行返回时出现空错误


驱动程序驱动程序= Driver(); // 打破静态上下文



}

private void test1()
{ // 此测试用例是来自文件bncwords.txt的随机单词,100个要选择的单词,最短长度= 3,最长的长度= 3

WordSearchPuzzle wordSearch = new WordSearchPuzzle( BasicEnglishWords.txt 50 2 9 ); // 在终端这一行是返回时出现空错误
wordSearch.getWordSearchList(); // 获取所选单词列表
wordSearch.showWordSearchPuzzle();

System.out.println(); // 打印拼图
}







请花些时间学习:



http://docs.oracle.com/javase/tutorial/java/ index.html [ ^ ]


I am using a driver to get words from a text file to use a wordsearch puzzle. I am new at using drivers and dont really know how to use them. The driver is not working and when i run my code as a whole i get an error java.lang.NullPointerException. I have put comments in where these errors are occuring within the public WordSearchPuzzle(String wordFile, int wordCount, int shortest, int longest) & within the driver. I have also included my method to get the words from the file.

import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.util.ArrayList;
import java.io.*;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
 

 

public class WordSearchPuzzle
 {
     private char[][] puzzle ;
     private ArrayList<String> puzzleWords ;
     
 
   
    public WordSearchPuzzle(String wordFile, int wordCount,
    int shortest, int longest) 
    {
        
        puzzleWords = loadWords(wordFile,shortest, longest);
        ArrayList<String> chosenWords = new ArrayList<String> ();
        int i=0;
        int sum=0;
        int gridSize =0;
        for(i=0;i<wordCount;i++)
        {
           int positionOfWord =(int)((Math.random()*puzzleWords.size()));// in terminal this line is coming back with a null error
            chosenWords.add(puzzleWords.get(positionOfWord));
        }
        
        for(i=0;i<chosenWords.size();i++)
        {
          sum+= chosenWords.get(i).length();
        }
        gridSize = sum *2;
        gridSize = (int)(Math.sqrt(gridSize));
        this.puzzle = new char[gridSize][gridSize];
        puzzleWords = chosenWords;
        
       
    }
    
 
    private ArrayList<String> loadWords(String wordFile,int shortest, int longest)
    {
        try 
        {
            FileReader aFileReader = new FileReader(wordFile);
            BufferedReader aBuffer = new BufferedReader(aFileReader);
            String fileLine;
            int lengthOfWord;
            ArrayList<String> words = new ArrayList<String>();
            fileLine = aBuffer.readLine();
            while(fileLine !=null)
            {
                lengthOfWord= fileLine.length();
                if(lengthOfWord >= shortest && lengthOfWord<= longest)
                {
                    words.add(fileLine);
            }
            fileLine = aBuffer.readLine();
        }
        aBuffer.close();
        aFileReader.close();
        return words;
       }
       catch(IOException x)
       {
        return null;
       }
    }
    
 
   
 
DRIVER........................................
 
import java.util.*;
import java.util.ArrayList;
 
public class Driver
{
     public static void main(String[] args)
    {
       // System.out.println();
       // System.out.println("**************************THE FOLLOWING ARE THE TEST FOR RANDOMLY GENERATED WORDS FROM GIVEN FILES********************************+|n") ;
        System.out.println();
        Driver.test1();//in terminal this line is coming back with a null error

       
       
    }
     private static void test1()
    {//this test case is one with random words from the file bncwords.txt, 100 words to be chosen, shortest length = 3, longest length = 3

        WordSearchPuzzle wordSearch = new WordSearchPuzzle("BasicEnglishWords.txt",50,2,9);//in terminal this line is coming back with a null error
        wordSearch.getWordSearchList() ;//gets the list of chose words
        wordSearch.showWordSearchPuzzle() ;
 
        System.out.println();//prints the puzzle
    }
     
}

解决方案

public class Driver
{
    public Driver(){ // konstruktor
        test1();
    }


    public static void main(String[] args)
    {
       // System.out.println();
       // System.out.println("**************************THE FOLLOWING ARE THE TEST FOR RANDOMLY GENERATED WORDS FROM GIVEN FILES********************************+|n") ;
        System.out.println();
        // Driver.test1();//in terminal this line is coming back with a null error


       Driver driver = new Driver(); // break out of static context
       
       
       
    }
    
    private void test1()
    {//this test case is one with random words from the file bncwords.txt, 100 words to be chosen, shortest length = 3, longest length = 3

        WordSearchPuzzle wordSearch = new WordSearchPuzzle("BasicEnglishWords.txt",50,2,9);//in terminal this line is coming back with a null error
        wordSearch.getWordSearchList() ;//gets the list of chose words
        wordSearch.showWordSearchPuzzle() ;
 
        System.out.println();//prints the puzzle
    }




Please take some time and learn:

http://docs.oracle.com/javase/tutorial/java/index.html[^]


这篇关于我正在为大学项目创建一个WordSearch拼图,并且存在空值问题。我不知道如何调用我的驱动程序测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆