اذهب إلى المحتوى
  • 0

خطا عند التعامل مع ملفات الاكسل في جافا

احمد صوالحة

السؤال

لدي برنامج يقوم بقراءة ملف اكسيل ويرفع محتوياته الى قاعدة البيانات وهو يعمل بشكل جيد لكن في حال كانت خلية ما فارغة يظهر لي الخطأ التالي:

java.lang.NullPointerException

هذا هو الكود الذي يحصل فيه الخطأ:

public void upload() throws IOException{
    String host="localhost";
    String port="543";
    String db_name="postgr";
    String username="root";
    String password="root";
    int max = 1000;
    int min = 0;
    String dir="C:\\Users\\User\\Desktop\\book.xlsx";
    int batchSize = 20;
    Connection connection = null;
    try {
        long start = System.currentTimeMillis();
        FileInputStream inputStream = new FileInputStream(dir);
        Workbook workbook = new XSSFWorkbook(inputStream);
        Sheet firstSheet = workbook.getSheetAt(0);
        Iterator<Row> rowIterator = firstSheet.iterator();
        try {
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        connection = DriverManager.getConnection("jdbc:postgresql://"+host+":"+port+"/"+db_name+"", ""+username+"", ""+password+"");
        connection.setAutoCommit(false);
        String sql = "INSERT INTO survey.tabledata (id,col1,col2,col3,col4,col5,col6,col7,col8,col9,col10,surveyid) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";
        PreparedStatement statement = connection.prepareStatement(sql);    
        int count = 0;
        while (rowIterator.hasNext()) {
            Row nextRow = rowIterator.next();
            Iterator<Cell> cellIterator = nextRow.cellIterator();
            while (cellIterator.hasNext()) {
                Cell nextCell = cellIterator.next();
                int columnIndex = nextCell.getColumnIndex();
                switch (columnIndex) {
                case 0:
                    int col1 = (int) nextCell.getNumericCellValue(); 
                    statement.setInt(1,col1);
                    break;
                case 1:
                    String col2 = nextCell.getStringCellValue();
                    statement.setString(2, col2);
                    break;
                case 2:
                    String col3 = nextCell.getStringCellValue();
                    statement.setString(3, col3);
                    break;
                case 3:
                    String col4 = nextCell.getStringCellValue();
                    statement.setString(4, col4);
                    break;
                case 4:
                    String col5 = nextCell.getStringCellValue();
                    statement.setString(5, col5);
                    break;
                case 5:
                    String col6 = nextCell.getStringCellValue();
                    statement.setString(6, col6);
                    break;
                case 6:
                    String col7 = nextCell.getStringCellValue();
                    statement.setString(7, col7);
                    break;
                case 7:
                    String col8 = nextCell.getStringCellValue();
                    statement.setString(8, col8);
                    break;
                case 8:
                    String col9 = nextCell.getStringCellValue();
                    statement.setString(9, col9);
                    break;

                case 9:
                    String col10 = nextCell.getStringCellValue();
                    statement.setString(10, col10);
                    break;
                case 10:
                    String col11 = nextCell.getStringCellValue();
                    statement.setString(11, col11);
                    break;
                case 11:
                    int col12 = (int) nextCell.getNumericCellValue();
                    statement.setInt(12,col12);
                    break;
                }
            }
            statement.addBatch();

            if (count % batchSize == 0) {
                statement.executeBatch();
            }              
        }
        statement.executeBatch();
        connection.commit();
        connection.close();
        long end = System.currentTimeMillis();
        System.out.printf("import done");

    } catch (IOException ex1) {
        System.out.println("Error");
        ex1.printStackTrace();
    } catch (SQLException ex2) {
        System.out.println("error");
        ex2.printStackTrace();
    }
}

كيف اتخطى تلك المشكلة ؟

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 1

كما ذكرت، يحدث الخطأ عند قراءة خلية فارغة، فعلينا وضع شرط للتأكد من احتواء الخلية على بيانات قبل قراءة القيمة منها

مثلا: قبل استخدام 

int col1 = (int) nextCell.getNumericCellValue();

لتصبح:

int col1 = -1;  قيمة ابتدائية غريبة عن بيانات الجدول

if (nextCell.getNumericCellValue() != null) {
     col1 = (int) nextCell.getNumericCellValue(); 
}

يمكنك تمرير قيم غير مستخدمة مثلا -1 في حالة int و سلسلة تحوي رمز معين في حال كان محتوى الخلية هو نص.

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0

السلام عليكم 

nullPointerException يحدث عندما تقوم باستخدام الvariable التي تكون عبارة عن object ولم يتم اسناد قيمة له وتقوم باستدعاء دالة بداخله.

يجب عليك قراءة الtrace حتى تتمكن من تحديد موقع الخطأ بالتحديد وبعدها سيكون حل المشكلة سهلاً او يمكنك أن تضع عبارات طباعة بعد كل عدد من العمليات لتعرف متى يحدث الخطأ بالتحديد بعدها ستكون مهمة حل الexception اسهل.

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...