问答详情
源自:2-4 Dao开发

for循环报错,代码和视频里都是一样的

package com.imooc.dao;

import com.imooc.domain.Student;
import org.junit.Test;
import java.util.List;

public class StudentDAOImplTest {

    @Test
    public void testQuery() {
        StudentDAO studentDAO = new StudentDAOImpl();
        List<Student> students = studentDAO.query();
        for (Student student : students){
            System.out.println("id:" + student.getId()
                                + ",name:" + student.getName()
                                + ",age:" + student.getAge());
        }
    }
}



package com.imooc.dao;
import com.imooc.domain.Student;

import java.util.List;

/*
StudengDAO访问接口
*/
public interface StudentDAO {
     /*
    查询所有学生
     */
    public List<Student> query();
}



package com.imooc.dao;

import com.imooc.domain.Student;
import com.imooc.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/*
StudentDAO访问接口实现类
 */
public class StudentDAOImpl implements StudentDAO{
    @Override
    public List<Student> query() {
        List<Student> students = new ArrayList<Student> ();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql="select * from student";
        try {
            connection = JDBCUtil.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();

            Student student = null;
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");

                student = new Student();
                student.setId(id);
                student.setName(name);
                student.setAge(age);

                students.add(student);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.release(resultSet,preparedStatement,connection);
        }
        return null;
    }
}

java.lang.NullPointerException	at com.imooc.dao.StudentDAOImplTest.testQuery(StudentDAOImplTest.java:14)	
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)	
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)	
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)	
at java.lang.reflect.Method.invoke(Method.java:498)	
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)	
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)	
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)	
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)	
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)	
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)	
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)	
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)	
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)	
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)	
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)	
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)	
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)	
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)	
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)	
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)	
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)	
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

报错的是第一段代码的for循环,怀疑是赋值没赋进去,有没有大佬帮我看一下


提问者:慕九州6528401 2018-07-30 19:24

个回答

  • qq_唯心_7
    2018-07-31 14:25:19
    已采纳

                    String name = resultSet.getString("name");
                    int age = resultSet.getInt("age");
    
                    student = new Student();
                    student.setId(id);
                    student.setName(name);
                    student.setAge(age);
    
                    students.add(student);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                JDBCUtil.release(resultSet,preparedStatement,connection);
            }
            return null;
        }
    }

    不好意思,看错了,你这个最后返回的是null,在foreach的时候对象为null,所以报错了

  • 慕容5618489
    2019-01-17 09:07:31

    public class StudentDAOImplTest {}这个方法里main少了实现接口,


  • qq_唯心_7
    2018-07-31 14:21:12

    https://img.mukewang.com/5b5fffc40001312119200925.jpg

    可以看一下圈红的地方

  • qq_唯心_7
    2018-07-31 13:12:03

    public class StudentDAOImplTest {
    
        @Test
        public void testQuery() {
            StudentDAO studentDAO = new StudentDAOImpl();

    你这段类没有实现接口啊,怎么使用多态调用方法的