我正在编写一个 Java 程序,它可以获取用户条目并将它们保存到 ArrayList 中,然后使用 ArrayList 打开一系列网页。该程序还应该能够从文件中读取网址。这就是我遇到问题的地方。
我目前正在接受:找不到文件 bills.txt。//文件在我的src文件夹中
Exception in thread "main" java.lang.NullPointerException
at PayBills.main(PayBills.java:92) //this is when the BufferdReader is closed
这不是家庭作业,但该程序与我即将完成的家庭作业共享概念,所以我不想改变任何关于我在文本中阅读方式的基本内容。任何建议表示赞赏!
import java.io.*;
import java.util.ArrayList;
public class PayBills implements Serializable
{
/*The purpose of this program is to assist the user in opening a series of webpages to
* pay bills online. The user will be able to save and edit a list of webpages,
* open the newly created list, or read a list from file.
*/
public static void main(String[] args) throws IOException
{
char input1;
String line = new String();
ArrayList<String> list1 = new ArrayList<String>();
Runtime rt = Runtime.getRuntime();
String filename = new String();
try
{
// print out the menu
rt.exec( "rundll32 url.dll,FileProtocolHandler " + "http://www.google.com");
printMenu();
// create a BufferedReader object to read input from a keyboard
InputStreamReader isr = new InputStreamReader (System.in);
BufferedReader stdin = new BufferedReader (isr);
do
{
System.out.println("\nWhat action would you like to perform?");
line = stdin.readLine().trim(); //read a line
input1 = line.charAt(0);
input1 = Character.toUpperCase(input1);
if (line.length() == 1) //check if a user entered only one character
{
switch (input1)
{
case 'A': //Add address process to array
System.out.println("\nPlease enter a web address to add to list:\n");
String str1 = stdin.readLine().trim();
if(str1.startsWith("http://www.") || str1.startsWith("https://www."))
list1.add(str1);
else
{
System.out.println("Please enter a valid web address (starting with http:// or https://).");
}
break;
case 'D': //Show current list
System.out.println(list1.toString());
break;
case 'E': //Execute current list
//rt.exec( "rundll32 url.dll,FileProtocolHandler " + "http://www.speedtest.net");
for(int i = 0; i < list1.size(); i++)
{
Process p1 = Runtime.getRuntime().exec("cmd /c start " + list1.get(i));
}
break;
case 'R': //Read list from file
System.out.println("\nPlease enter the filename to read: ");
{
filename = stdin.readLine().trim();
}
FileReader fr = null;
BufferedReader inFile = null;
try
{
fr = new FileReader(filename);
inFile = new BufferedReader(fr);
line = inFile.readLine();
System.out.println("Test2");
while(line != null)
{
System.out.println("Test3");
if(line.startsWith("http://www.") == false || line.startsWith("https://www.") == false)
System.out.println("Error: File not in proper format.");
else
list1.add(line);
}
System.out.println(filename + " was read.");
}
catch(FileNotFoundException exception)
{
System.out.println("The file " + filename + " was not found.");
break;
}
catch(IOException exception)
{
System.out.println("Error. " + exception);
}
finally
{
inFile.close();
}
break;
case '?': //Display Menu
printMenu();
break;
case 'Q': //Quit
System.out.println("Goodbye!");
System.exit(0);
}//end switch
}//end if
else
System.out.print("Unknown action\n");
}//end do
while (input1 != 'Q' || line.length() != 1);
}
catch(IOException e1)
{
System.out.println("Error: " + e1);
}
}//end main
public static void printMenu()
{
System.out.print("Choice\t\tAction\n" +
"------\t\t------\n" +
"A\t\tAdd Web Address to List\n" +
"D\t\tDisplay Current List\n" +
"E\t\tExecute Current List\n" +
"R\t\tRead List from File\n" +
"?\t\tDisplay Menu\n" +
"Q\t\tQuit\n");
}//end of printMenu()
}//end PayBills
编辑:好的,程序不再崩溃,因为我修复了 NPE,但我仍然收到“找不到文件 bills.txt。”,这是捕获的异常。如上所述,该文件位于我的 src 文件夹中,因此路径应该是正确的。