0

I want to call the output of dir1 method in main method. Actually I want the output of dir1 method in a JSP. where it helps me print the output in jsp page. But first I am unable to so in main method.

Facing Null pointer exception in main method during runtime since the program compiles correctly.

  import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;

    public class test_1 {
           String[] b;
        public String dir1  (String ps,String file1)
        {   
            String result="";

            int i=0;
        try{    
    File file=new File(file1);

    ProcessBuilder pb = new ProcessBuilder("cmd","/C",ps);

    pb.directory(file);

    Process p = pb.start();

    BufferedReader reader = new BufferedReader(new InputStreamReader(
            p.getInputStream()));
    String readline;

    while ((readline = reader.readLine()) != null) 
    {  
       this.b[i]=readline;
       i++;
    }
    int exitValue = p.waitFor();

        }
        catch(InterruptedException e)
        {
            e.printStackTrace();

        }
        catch(Exception e)
        {
            System.out.println(e);

        }

    /*try {
    int exitValue = p.waitFor();
    } 

    catch (InterruptedException e) {
    e.printStackTrace();
    }*/


    /*finally{
    out.close();  
    } */        
            return result;

        }
        public static void main(String[] args)
        {
            test_1 f1=new test_1();
            f1.dir1("dir", "C:\\Program Files\\test\\test.txt");
            for(int i=0;i<100;i++)
            {
                System.out.println(f1.b[i]);
            }
            System.out.println(f1.dir1("dir", "C:\\Program Files\\test\\test.txt"));
        }
    }
Maninder Singh
  • 135
  • 4
  • 12

1 Answers1

1

You need to initialize the b array in test_1 class.

String[] b = new String[size];

Read More about this exception here and how to fix.

Community
  • 1
  • 1
Salah
  • 8,567
  • 3
  • 26
  • 43