2015-01-28 2 views
0

Я пытаюсь отправить данные HTML в свою базу данных с помощью сервлетов. Пожалуйста, помогите мне!Состояние Apache 500: Исключение нулевого указателя

Это моя doGet функция

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    // TODO Auto-generated method stub 
    Student st=new Student(); 
    st.setId(Integer.parseInt(request.getParameter("id"))); 
    st.setName(request.getParameter("name")); 
    DBConnection db= new DBConnection(); 
    db.addStudent(st); 
} 

Это моя addStudent функция

public class DBConnection { 

private static final String driverName = "com.mysql.jdbc.Driver"; 
static final String URL = "jdbc:mysql://localhost:3306/Test1"; 
private static final String username = "root"; 
private static final String password = "root"; 

public static Connection getConnection() { 
    System.out.println("HEre"); 
    Connection connection = null; 
    try { 
     Class.forName(driverName); 
     connection = DriverManager.getConnection(URL, username, password); 
    } catch (ClassNotFoundException e) { 
     e.printStackTrace(); 
    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 
    return connection; 
} 

public void addStudent(Student st) { 
    Connection connection = null; 
    PreparedStatement preparedStatement = null; 
    try { 
     System.out.println(st.getId() + st.getName()); 
     String Query = "INSERT INTO DETAILS VALUES (? , ? , ?)"; 
     connection = getConnection(); 
     preparedStatement = connection.prepareStatement(Query); 
     preparedStatement.setInt(1, st.getId()); 
     preparedStatement.setString(2, st.getName()); 
     preparedStatement.setBlob(3, (Blob) null); 
     preparedStatement.executeUpdate(); 
    } 

Это исключение, которое я получаю, когда я ударил представить:

type Exception report 

message description The server encountered an internal error that prevented it from fulfilling this request. 

exception java.lang.NullPointerException 
    DataAccessObject.DBConnection.addStudent(DBConnection.java:42) 
    ActionObject.Input.doGet(Input.java:36) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) 

Это мой апач серверные журналы:

127.0.0.1 - - [28/Jan/2015:19:13:02 +0530] "GET /Test1/Input?name=&id=&mydropdown=Coding&mydropdown=Coding&marks=&submitbtn=Submit HTTP/1.1" 500 1478 
127.0.0.1 - - [28/Jan/2015:20:09:15 +0530] "GET/HTTP/1.1" 200 11243 
+2

Что является линия 42 в 'DBConnection'? Я * угадываю * объект 'Connection' не был инициализирован должным образом. Вам придется отлаживать ваше приложение. –

+0

@NikosParaskevopoulos Я отредактировал мое сообщение, чтобы показать больше кода. Это строка: 'prepareStatement = connection.prepareStatement (Query);' – divinediu

+1

Итак, соединение это null. Проверьте функцию getConnection() – Alist3r

ответ

1

Добавляет MySQL-разъем-java.jar в проект:

enter image description here

Затем добавьте баночку в Java путь сборки проекта:

enter image description here

Смежные вопросы