java.util.Scanner 对象在解析文本文件时抛出 NullPointerException
java.util.Scanner object throws NullPointerException while parsing text file
我为 this Google Code Jam question 创建了这个 JAVA 代码。
当我执行代码和 select this text file 时,它抛出 NullPointerException。请帮忙。我想这是因为 nextLine() 将扫描仪定位到 nextLine 并且 returns 当前行的其余部分为空。
line = ab.nextLine();
如何修复此错误以便它读取下一行文本?提供代码和您的建议。我会感激你一辈子:)
/* JEditorPaneFileChooser.java
* Copyright (c) 2014, HerongYang.com, All Rights Reserved.
*/
import java.io.*;
import java.nio.*;
import java.nio.charset.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import java.util.Scanner;
public class JEditorPaneFileChooser implements ActionListener {
JFrame myFrame = null;
JEditorPane myPane = null;
JScrollPane mySPane = null;
JMenuItem cmdOpen = null;
JMenuItem cmdSave = null;
String dirName = "\herong\swing\";
String fileName = "";
public static void main(String[] a) {
(new JEditorPaneFileChooser()).test();
}
private void test() {
myFrame = new JFrame("JEditorPane JFileChooser Test");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(300,400);
myPane = new JEditorPane();
myPane.setContentType("text/plain");
myPane.setText(
"CLICK ON \'File\' AND SELECT \'Open\'\nTHEN SELECT \'A-large-practice.in\'");
mySPane = new JScrollPane(myPane);
myFrame.setContentPane(mySPane);
JMenuBar myBar = new JMenuBar();
JMenu myMenu = getFileMenu();
myBar.add(myMenu);
myFrame.setJMenuBar(myBar);
myFrame.setVisible(true);
}
private JMenu getFileMenu() {
JMenu myMenu = new JMenu("File");
cmdOpen = new JMenuItem("Open");
cmdOpen.addActionListener(this);
myMenu.add(cmdOpen);
return myMenu;
}
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File(dirName));
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
FileNameExtensionFilter filter = new FileNameExtensionFilter(
".in file", "in");
chooser.setFileFilter(filter);
Object cmd = e.getSource();
try {
if (cmd == cmdOpen) {
int code = chooser.showOpenDialog(myPane);
if (code == JFileChooser.APPROVE_OPTION) {
File selectedFile = chooser.getSelectedFile();
long ag= selectedFile.length();
fileName = selectedFile.getName();
FileInputStream fis =
new FileInputStream(selectedFile);
InputStreamReader in=
new InputStreamReader(fis, Charset.forName("UTF-8"));
char[] buffer=new char[(int)ag];
int n = in.read(buffer);
String text = new String(buffer, 0, n);
String ans =bag(text);
myPane.setText(ans);
in.close();
}
}
} catch (Exception f) {
f.printStackTrace();
}
}
public static String bag(String input){
Scanner ab=new Scanner(input);
int case1,lp=0;
case1 = ab.nextInt();
int total, k;
char[][] chari,m=null,n=null;
int R1[][]=null,B1[][]=null;
String line="",line1="",ghost="",answ="";
int axr=0,minr=0,maxr=0,axrb=0,minrb=0,maxrb=0,xr=0,lr=0,nex=0,xrb=0,lrb=0,nexb=0,yr=0,lr1=0,lr2=0,lr3=0,yrb=0,lr1b=0,lr2b=0,lr3b=0,man=0,min=0,
manb=0,minb=0,rax=0,ray=0,cra=0,raxb=0,rayb=0,crab=0;
int t,i,j,x,p,q,p1,g,h,y,w,sumit,sum,RX=0,BX=0;
for (i=1;i<=case1;i++)
{
total = ab.nextInt();
k=ab.nextInt();
chari=new char[total][total];
for(x=0;x<total;x++)
{
line = ab.nextLine();
for (y=0;y<total;y++)
{ chari[x][y] =line.charAt(y);}
}
for (g=0;g<total;g++)
{
w=0;
for(h=total-1;h>=0;h--)
{
m[g][w]=chari[h][g];
w++;
}
}
for (q=0;q<total;q++)
{
p1=total-1;
for (p=total-1;p>=0;p--)
{
if( (m[p][q]=='R') || (m[p][q]=='B'))
{
n[p1][q]=m[p][q];
p--;}
}
}
for (sumit=total-1;sumit>=0;sumit--)
{
for (sum=0;sum<total;sum++)
{
if (n[sumit][sum] == 'R')
{
R1[RX][0]=sumit;
R1[RX][3]=sum;
RX++;
}
else if (n[sumit][sum]=='B')
{ B1[BX][0] = sumit;
B1[BX][4]= sum;
BX++;
}
}
}
axr=R1.length;
for (minr=0;minr<axr;minr++)
{
xr = R1 [minr][0];
for (maxr=minr+1;maxr<axr;maxr++)
{
if (R1 [maxr][0] == xr)
lr++;}
if (lr>=k) nex=1;
if (lr<=k) break;
else lr=0;
}
if (nex!=1)
{
for (minr=0;minr<axr;minr++)
{
yr=R1[minr][5];
for (maxr=minr+1;maxr<axr;maxr++)
{
if (R1[maxr][6]==yr)
lr1++;}
if (lr1>=k)nex=1;
if (lr1>=k) break;
else lr1=0;
}
if (nex!=1)
{
for (man=0;man<axr;man++)
{
rax=R1[man][0];
ray = R1 [man][7];
cra=1;
for (min=man+1;min<axr;min++)
{
if ( (R1[min][0]==rax-cra) && (R1[min][8] == ray +cra) )
{lr2++;
cra++;}
else break;
}
cra=1;
if (lr2>=k) nex=1;
if (lr2>=k) break;
else lr2=0;
}
if (nex!=1)
{
for (man=0;man<axr;man++)
{
rax=R1[man][0];
ray = R1 [man][9];
cra=1;
for (min=man+1;min<axr;min++)
{
if ( (R1[min][0]==rax-cra) && (R1[min][10] == ray -cra) )
{lr3++;
cra++;}
else break;
}
cra=1;
if (lr3>=k) nex=1;
if (lr3>=k) break;
else lr3=0;
}
}
}
}
axrb=B1.length;
for (minrb=0;minrb<axrb;minrb++)
{
xrb = B1 [minrb][0];
for (maxrb=minrb+1;maxrb<axrb;maxrb++)
{
if (B1 [maxrb][0] == xrb)
lr++;}
if (lrb>=k) nexb=1;
if (lrb<=k) break;
else lr=0;
}
if (nexb!=1)
{
for (minrb=0;minrb<axrb;minrb++)
{
yr=B1[minrb][11];
for (maxrb=minrb+1;maxrb<axrb;maxrb++)
{
if (B1[maxrb][12]==yrb)
lr1b++;}
if (lr1b>=k)nexb=1;
if (lr1b>=k) break;
else lr1b=0;
}
if (nexb!=1)
{
for (manb=0;manb<axrb;manb++)
{
raxb=B1[manb][0];
rayb = B1 [manb][13];
crab=1;
for (minb=manb+1;minb<axrb;minb++)
{
if ( (B1[minb][0]==raxb-crab) && (B1[minb][14] == rayb +crab) )
{lr2b++;
crab++;}
else break;
}
crab=1;
if (lr2b>=k) nexb=1;
if (lr2b>=k) break;
else lr2b=0;
}
if (nexb!=1)
{
for (manb=0;manb<axrb;manb++)
{
raxb=B1[manb][0];
rayb = B1 [manb][15];
crab=1;
for (minb=manb+1;minb<axrb;minb++)
{
if ( (B1[minb][0]==raxb-crab) && (B1[minb][16] == rayb -crab) )
{lr3b++;
crab++;}
else break;
}
crab=1;
if (lr3b>=k) nexb=1;
if (lr3b>=k) break;
else lr3b=0;
}
}
}
}
if (nex==1)
{ghost=(nexb==1)?"Both": "Red";}
else
{ghost =(nexb==1)? "Blue":"Neither";}
answ= "case1 #" + i+ ": " +ghost+"\n";
}
return answ;
}
}
堆栈跟踪--
java.lang.NullPointerException
at JEditorPaneFileChooser.bag(JEditorPaneFileChooser.java:112)
at JEditorPaneFileChooser.actionPerformed(JEditorPaneFileChooser.java:78)
NullPointerException 不在
line = ab.nextLine()
它在
chari[x][y] =line.charAt(y);
原因是 chari 为空。
我为 this Google Code Jam question 创建了这个 JAVA 代码。 当我执行代码和 select this text file 时,它抛出 NullPointerException。请帮忙。我想这是因为 nextLine() 将扫描仪定位到 nextLine 并且 returns 当前行的其余部分为空。
line = ab.nextLine();
如何修复此错误以便它读取下一行文本?提供代码和您的建议。我会感激你一辈子:)
/* JEditorPaneFileChooser.java
* Copyright (c) 2014, HerongYang.com, All Rights Reserved.
*/
import java.io.*;
import java.nio.*;
import java.nio.charset.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import java.util.Scanner;
public class JEditorPaneFileChooser implements ActionListener {
JFrame myFrame = null;
JEditorPane myPane = null;
JScrollPane mySPane = null;
JMenuItem cmdOpen = null;
JMenuItem cmdSave = null;
String dirName = "\herong\swing\";
String fileName = "";
public static void main(String[] a) {
(new JEditorPaneFileChooser()).test();
}
private void test() {
myFrame = new JFrame("JEditorPane JFileChooser Test");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(300,400);
myPane = new JEditorPane();
myPane.setContentType("text/plain");
myPane.setText(
"CLICK ON \'File\' AND SELECT \'Open\'\nTHEN SELECT \'A-large-practice.in\'");
mySPane = new JScrollPane(myPane);
myFrame.setContentPane(mySPane);
JMenuBar myBar = new JMenuBar();
JMenu myMenu = getFileMenu();
myBar.add(myMenu);
myFrame.setJMenuBar(myBar);
myFrame.setVisible(true);
}
private JMenu getFileMenu() {
JMenu myMenu = new JMenu("File");
cmdOpen = new JMenuItem("Open");
cmdOpen.addActionListener(this);
myMenu.add(cmdOpen);
return myMenu;
}
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File(dirName));
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
FileNameExtensionFilter filter = new FileNameExtensionFilter(
".in file", "in");
chooser.setFileFilter(filter);
Object cmd = e.getSource();
try {
if (cmd == cmdOpen) {
int code = chooser.showOpenDialog(myPane);
if (code == JFileChooser.APPROVE_OPTION) {
File selectedFile = chooser.getSelectedFile();
long ag= selectedFile.length();
fileName = selectedFile.getName();
FileInputStream fis =
new FileInputStream(selectedFile);
InputStreamReader in=
new InputStreamReader(fis, Charset.forName("UTF-8"));
char[] buffer=new char[(int)ag];
int n = in.read(buffer);
String text = new String(buffer, 0, n);
String ans =bag(text);
myPane.setText(ans);
in.close();
}
}
} catch (Exception f) {
f.printStackTrace();
}
}
public static String bag(String input){
Scanner ab=new Scanner(input);
int case1,lp=0;
case1 = ab.nextInt();
int total, k;
char[][] chari,m=null,n=null;
int R1[][]=null,B1[][]=null;
String line="",line1="",ghost="",answ="";
int axr=0,minr=0,maxr=0,axrb=0,minrb=0,maxrb=0,xr=0,lr=0,nex=0,xrb=0,lrb=0,nexb=0,yr=0,lr1=0,lr2=0,lr3=0,yrb=0,lr1b=0,lr2b=0,lr3b=0,man=0,min=0,
manb=0,minb=0,rax=0,ray=0,cra=0,raxb=0,rayb=0,crab=0;
int t,i,j,x,p,q,p1,g,h,y,w,sumit,sum,RX=0,BX=0;
for (i=1;i<=case1;i++)
{
total = ab.nextInt();
k=ab.nextInt();
chari=new char[total][total];
for(x=0;x<total;x++)
{
line = ab.nextLine();
for (y=0;y<total;y++)
{ chari[x][y] =line.charAt(y);}
}
for (g=0;g<total;g++)
{
w=0;
for(h=total-1;h>=0;h--)
{
m[g][w]=chari[h][g];
w++;
}
}
for (q=0;q<total;q++)
{
p1=total-1;
for (p=total-1;p>=0;p--)
{
if( (m[p][q]=='R') || (m[p][q]=='B'))
{
n[p1][q]=m[p][q];
p--;}
}
}
for (sumit=total-1;sumit>=0;sumit--)
{
for (sum=0;sum<total;sum++)
{
if (n[sumit][sum] == 'R')
{
R1[RX][0]=sumit;
R1[RX][3]=sum;
RX++;
}
else if (n[sumit][sum]=='B')
{ B1[BX][0] = sumit;
B1[BX][4]= sum;
BX++;
}
}
}
axr=R1.length;
for (minr=0;minr<axr;minr++)
{
xr = R1 [minr][0];
for (maxr=minr+1;maxr<axr;maxr++)
{
if (R1 [maxr][0] == xr)
lr++;}
if (lr>=k) nex=1;
if (lr<=k) break;
else lr=0;
}
if (nex!=1)
{
for (minr=0;minr<axr;minr++)
{
yr=R1[minr][5];
for (maxr=minr+1;maxr<axr;maxr++)
{
if (R1[maxr][6]==yr)
lr1++;}
if (lr1>=k)nex=1;
if (lr1>=k) break;
else lr1=0;
}
if (nex!=1)
{
for (man=0;man<axr;man++)
{
rax=R1[man][0];
ray = R1 [man][7];
cra=1;
for (min=man+1;min<axr;min++)
{
if ( (R1[min][0]==rax-cra) && (R1[min][8] == ray +cra) )
{lr2++;
cra++;}
else break;
}
cra=1;
if (lr2>=k) nex=1;
if (lr2>=k) break;
else lr2=0;
}
if (nex!=1)
{
for (man=0;man<axr;man++)
{
rax=R1[man][0];
ray = R1 [man][9];
cra=1;
for (min=man+1;min<axr;min++)
{
if ( (R1[min][0]==rax-cra) && (R1[min][10] == ray -cra) )
{lr3++;
cra++;}
else break;
}
cra=1;
if (lr3>=k) nex=1;
if (lr3>=k) break;
else lr3=0;
}
}
}
}
axrb=B1.length;
for (minrb=0;minrb<axrb;minrb++)
{
xrb = B1 [minrb][0];
for (maxrb=minrb+1;maxrb<axrb;maxrb++)
{
if (B1 [maxrb][0] == xrb)
lr++;}
if (lrb>=k) nexb=1;
if (lrb<=k) break;
else lr=0;
}
if (nexb!=1)
{
for (minrb=0;minrb<axrb;minrb++)
{
yr=B1[minrb][11];
for (maxrb=minrb+1;maxrb<axrb;maxrb++)
{
if (B1[maxrb][12]==yrb)
lr1b++;}
if (lr1b>=k)nexb=1;
if (lr1b>=k) break;
else lr1b=0;
}
if (nexb!=1)
{
for (manb=0;manb<axrb;manb++)
{
raxb=B1[manb][0];
rayb = B1 [manb][13];
crab=1;
for (minb=manb+1;minb<axrb;minb++)
{
if ( (B1[minb][0]==raxb-crab) && (B1[minb][14] == rayb +crab) )
{lr2b++;
crab++;}
else break;
}
crab=1;
if (lr2b>=k) nexb=1;
if (lr2b>=k) break;
else lr2b=0;
}
if (nexb!=1)
{
for (manb=0;manb<axrb;manb++)
{
raxb=B1[manb][0];
rayb = B1 [manb][15];
crab=1;
for (minb=manb+1;minb<axrb;minb++)
{
if ( (B1[minb][0]==raxb-crab) && (B1[minb][16] == rayb -crab) )
{lr3b++;
crab++;}
else break;
}
crab=1;
if (lr3b>=k) nexb=1;
if (lr3b>=k) break;
else lr3b=0;
}
}
}
}
if (nex==1)
{ghost=(nexb==1)?"Both": "Red";}
else
{ghost =(nexb==1)? "Blue":"Neither";}
answ= "case1 #" + i+ ": " +ghost+"\n";
}
return answ;
}
}
堆栈跟踪--
java.lang.NullPointerException
at JEditorPaneFileChooser.bag(JEditorPaneFileChooser.java:112)
at JEditorPaneFileChooser.actionPerformed(JEditorPaneFileChooser.java:78)
NullPointerException 不在
line = ab.nextLine()
它在
chari[x][y] =line.charAt(y);
原因是 chari 为空。