0
import java.io.FileInputStream;
import java.util.ArrayList;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class choice_checkOut {

    public static String ID;
    public static String studenName;
    public static String PH;
    public static String bookName;
    public static String returnVal;

    public static ArrayList<String> DB_ID;
    public static ArrayList<String> DB_studentName;
    public static ArrayList<String> DB_PH;
    public static ArrayList<String> DB_bookName;
    public static ArrayList<String> DB_returnVal;

    String value;

    public void get_Val()
    {
        // TODO Auto-generated constructor stub
        try 
        {
            value= "";
            String a = "C:/Users/ehdtj/OneDrive/바탕 화면/Web/bookControl/UI/info.xlsx";
            FileInputStream file = new FileInputStream(a);
            @SuppressWarnings("resource")
            XSSFWorkbook workbook = new XSSFWorkbook(file);
            XSSFSheet sheet = workbook.getSheetAt(0); 
            for(int rowIndex=1; rowIndex<=50; rowIndex++)
            {
                XSSFRow row = sheet.getRow(rowIndex);
                if(row != null)
                {
                    for(int columnINdex = 0; columnINdex <=5; columnINdex++) 
                    {
                        XSSFCell cell = row.getCell(columnINdex);

                        if (cell == null)
                        {
                            continue;
                        }

                        else
                        {
                            switch(cell.getCellType())
                            {
                            case NUMERIC://XSSFCell.CELL_TYPE_NUMERIC:
                                value = cell.getStringCellValue()+"";
                                break;
                            case STRING://XSSFCell.CELL_TYPE_STRING:
                                value = cell.getStringCellValue()+"";
                                break;
                            case BLANK://XSSFCell.CELL_TYPE_BLANK:
                                value = "nell";
                                break;
                            default:
                                break;
                            }

                            switch(columnINdex)
                            {
                            case 0:
                                ID = value;
                                break;
                            case 1:
                                studenName = value;
                                break;
                            case 2:
                                PH= value;
                                break;
                            case 3:
                                bookName = value;
                                break;
                            case 4:
                                returnVal = value;
                                break;
                            default:
                                break;
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

    public void classcification()
    {       
        get_Val();
        for(int i=0; i<=50; i++)
        {
            switch(i)
            {
            case 0:
                DB_ID.add(ID);
                break;
            case 1:
                DB_studentName.add(studenName);
                break;
            case 2:
                DB_PH.add(PH);
                break;
            case 3:
                DB_bookName.add(bookName);
                break;
            case 4:
                DB_returnVal.add(returnVal);
                break;
            default:
                break;
            }
        }
    }

    public ArrayList<String> RE_ID()
    {
        classcification();
        return DB_ID;
    }

    public  ArrayList<String> RE_bookName ()
    {
        return DB_bookName;
    }

    public static void main(String[] arg)
    {
        choice_checkOut test = new choice_checkOut();

            ArrayList<String> a = new ArrayList<String>();
            a = test.RE_ID();
            String b = a.get(0);
            System.out.println(b);
    }
} //class

Exception in thread "main" java.lang.NullPointerException

  1. Value is static
  2. Value is public
  3. I used for
  4. I checked value in get_Val().
  5. I checked value in classcification(). but for is not work.
  6. I used this.value. but this.value is not work.

i try....

        //ArrayList<String> a = test.RE_ID();

        //System.out.println(a);
        //System.out.println(a.size());
        /*
        for(int i=0; i<= a.size(); i++)
        {
            String b= a.get(i);
            System.out.println(b);
        }
        */
        //System.out.println(DB_ID);
        //System.out.println(test.DB_bookName);
        /*
        int index= 0;
        try {
            String a = test.print_ID(index);
            String b = test.print_bookName(index);
            System.out.println(a+b);
        } catch (Exception e) {
            // TODO: handle exception
            String b = test.print_bookName(index);
            System.out.println(b);
        }
        */

finally

I used arraylist.get() n String b initiated.

why arrayList b is null?

feelSoWiF
  • 41
  • 1
  • 3
  • The iteration limit should be `i < a.size();`, not `i<= a.size();` and the values inside of array can be null. (e.q. a.add(null) is valid) – Butiri Dan Mar 14 '20 at 11:48
  • The stack trace of your NullPointerException tells you exactly where the problem is. – VGR Mar 14 '20 at 14:46

1 Answers1

1

First of all, your code is a huge mess. Instead of working with one huge method, divide your code into different methods. It is easier to read, easier to detect bugs with and looks more professional.

Second of all, at no point do you initialize any of your ArrayLists, an ArrayList cannot just be declared and then instantly be ready to have values added to, you have to first initialize it: ArrayList<String> arrayList = new ArrayList<>();

This applies to every single Object in Java, you cannot just declare an Object and instantly start calling its methods, you have to first call its constructor (or, alternatively, a method that returns an Object of the same type).

Eldar B.
  • 1,097
  • 9
  • 22