0

I created this JAVA code for this Google Code Jam question. When i execute the code and select this text file, it throws NullPointerException. Please help. I guess it is because nextLine() positions the scanner to the nextLine and returns the rest of the current line which is empty.

    line = ab.nextLine();

How do I fix this error so that it reads the next line of text? Provide code along with your suggestions. I will be grateful to you for the rest of my life :)

            /* 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;
        }

    }

stacktrace --

java.lang.NullPointerException
at JEditorPaneFileChooser.bag(JEditorPaneFileChooser.java:112)
at JEditorPaneFileChooser.actionPerformed(JEditorPaneFileChooser.java:78)
4
  • 1
    What does the stacktrace tell you? Commented Jan 26, 2015 at 15:39
  • 1
    possible duplicate of What is a Null Pointer Exception, and how do I fix it? Commented Jan 26, 2015 at 15:40
  • you need to provide stack trace , which line threw the exception?? Commented Jan 26, 2015 at 15:41
  • this statement in bag() ---> line = ab.nextLine(); Commented Jan 26, 2015 at 15:45

1 Answer 1

1

The NullPointerException is not in

 line = ab.nextLine()

it is in

 chari[x][y] =line.charAt(y);

The reason is, that chari is null.

Sign up to request clarification or add additional context in comments.

6 Comments

None of the array objects char chari[][]=null,m[][]=null,n[][]=null;int R1[][]=null,B1[][]=null; are created properly.
i've tried initializing ----- chari[][] =new char[total][total] ----------but it still throws NullPointerException at the same line. [code link] (gist.github.com/sumitaich/b2991ec122f2db847cf1)
OK - see my earlier comment: not only chari but also m, n, R1, B1 need to be created properly.
1. I cannot access the code link to see the initializing, but i guess you get another error because nextLine positions the scanner to the nextLine and returns the rest of the current line which is empty.
2. If you fix this error, you will get the next NullPointer a little later until you initialize your other arrays as laune wrote.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.