By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,799 Members | 1,403 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,799 IT Pros & Developers. It's quick & easy.

Exception with String array in Java

sunsolaris2000
P: 20
I have 3 sql tables: **professors(prof_id, name)**, **professors_courses(prof_id, course_id)** and **courses(course_id, title)**.

**professors_courses** is the bridge table between professors and courses.

In *CourseAssignments.java* I search which courses belong to professor x, after user enters a professor "name".

**Course.java**

Expand|Select|Wrap|Line Numbers
  1.   public class Course {
  2.         private int course_id;
  3.         private String title;
  4.  
  5.         public Course(int id, String title){
  6.             super();
  7.             this.course_id = id;
  8.             this.title = title;
  9.         }
  10.  
  11.         public int getCourse_id(){
  12.             return course_id;
  13.         }
  14.  
  15.         public String getTitle(){
  16.             return title;
  17.         }

Expand|Select|Wrap|Line Numbers
  1. Same with **Professor.java**:
  2.  
  3.     public class Professor {
  4.        private int prof_id;
  5.        private String name;
...................

Expand|Select|Wrap|Line Numbers
  1. Same with **PC.java**:
  2.  
  3.     public class PC {
  4.       private int prof_id;
  5.       private int course_id;
..................

**
Expand|Select|Wrap|Line Numbers
  1. ArrayList<PC> results** contains prof_id <-> course_id
**
Expand|Select|Wrap|Line Numbers
  1. ArrayList<Course> courses** contains course_id <-> title (identical with the table "courses")
I need these for example because let's say:

In "professors" table we have prof_id: 5, name: John;
In "professors_courses" we have prof_id: 5, course_id: 1
prof_id: 5, course_id: 9
In "courses" table we have course_id: 1, title: French
course_id: 9, title: Spanish

User enters: "John"

In ArrayList "results" I store:
prof_id: 5, course_id: 1 and prof_id: 5, course_id: 9
So it's similar with the bridge table, but only contains the info regarding "John".

In String cs[] I want to store all the courses of "John", so cs[] = {"French", "Spanish"}

All in all I want to take only the "title" fields from ArrayList "courses" and store them into a String array (cs[]).

In JSP I would display the titles:

Expand|Select|Wrap|Line Numbers
  1.    <%     
  2.         //all the courses that a professor has
  3.         String titles[] = CourseAssignments.getInstance().bridge1("John");
  4.         %>
  5.  
  6.         <html>
  7.            <head></head>
  8.              <body>
  9.              <center>
  10.                 <table border="1" cellspacing="1" cellpadding="8" bgcolor= "33FFCC"> 
  11.                     <tr>
  12.                         <td bgcolor= #FF9966><b>Course</b></td>
  13.                     </tr>
  14.                          <%
  15.                         for (int i = 0; i < titles.length; i++){
  16.                             if(titles[i] != null){
  17.                         %>
  18.                         <tr>
  19.                           <td><%= titles[i]%></td>
  20.                         </tr>
  21.                         <%
  22.                              }
  23.                         }
  24.         %>
  25.                     </table>
I have Exception at the array:

org.apache.jasper.JasperException: java.lang.NullPointerException
Jul 20 '12 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.