Advertisement
MartinYanchev-99

NullPointer in SQLDAO

Dec 8th, 2020
869
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1. package daos;
  2.  
  3. import java.sql.Statement;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import model.Route;
  11. import model.RouteStage;
  12.  
  13. public class SQLDAO extends DAO {
  14.     static private String userName = "root";
  15.    static private String password = "";
  16.    static private String url =
  17.                   "jdbc:mysql://localhost:8080/routes";
  18.    static private Connection connection = null;
  19.    static private Statement stmt = null;
  20.    static private ResultSet rs = null;  
  21.    public SQLDAO() {
  22.        try{
  23.             connection = DriverManager.getConnection(url,userName,password);
  24.             stmt = connection.createStatement();
  25.        }catch (SQLException e){
  26.             System.out.println(e.getMessage());       }
  27.  
  28. }
  29.  
  30.      
  31.        
  32.  
  33.     @Override
  34.     public List<Route> getRoutes(){
  35.         List<Route> routes = new ArrayList<>();
  36.         try{
  37.             String query = "CALL GetRoutes()";
  38.             rs = stmt.executeQuery(query);
  39.             while(rs.next()){
  40.                 int id = rs.getInt("ROUTEID");
  41.                 String routeTitle = rs.getString("ROUTETITLE");
  42.                 String departurePoint = rs.getString("DEPARTUREPOINT");
  43.                 Route route = new Route(id,routeTitle,departurePoint);
  44.  
  45.  
  46.                 List<RouteStage> stages = new ArrayList<>();
  47.                 String query2 = "CALL GetRouteStages( "+ id+" )";
  48.                 Statement stmt2 = connection.createStatement();
  49.                 ResultSet rs2 = stmt2.executeQuery(query2);
  50.                 while(rs2.next()){
  51.                    
  52.                     int stageNumber = rs2.getInt("STAGENUMBER");
  53.                     String endPoint = rs.getString("ENDPOINT");
  54.                     float stageLength = rs2.getFloat("STAGELENGTH");
  55.                     int routeId = rs2.getInt("ROUTEID");
  56.  
  57.                     RouteStage stage = new RouteStage(stageNumber, endPoint, stageLength,routeId);
  58.                     stages.add(stage);
  59.  
  60.                 }
  61.                 route.setRouteStages(stages);
  62.                 routes.add(route);
  63.             }
  64.         }catch(SQLException e){
  65.             System.out.println(e);
  66.  
  67.         }
  68.         return routes;
  69.  
  70.     }
  71.  
  72.     @Override
  73.     public void addRoute(Route route) {
  74.         // TODO Auto-generated method stub
  75.  
  76.     }
  77.  
  78.     @Override
  79.     public void addStage(RouteStage stage) {
  80.         // TODO Auto-generated method stub
  81.  
  82.     }
  83.  
  84.     @Override
  85.     public Route getRoute(int routeId) {
  86.         // TODO Auto-generated method stub
  87.         return null;
  88.     }
  89.  
  90.    
  91.      
  92.  
  93.  
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement