Exception with String array in Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sunsolaris2000
    New Member
    • Jan 2012
    • 20

    Exception with String array in Java

    I have 3 sql tables: **professors(pr of_id, name)**, **professors_co urses(prof_id, course_id)** and **courses(cours e_id, title)**.

    **professors_co urses** is the bridge table between professors and courses.

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

    **Course.java**

    Code:
      public class Course {
            private int course_id;
            private String title;
            
            public Course(int id, String title){
            	super();
            	this.course_id = id;
            	this.title = title;
            }
            
            public int getCourse_id(){
            	return course_id;
            }
            
            public String getTitle(){
            	return title;
            }

    Code:
    Same with **Professor.java**:
    
        public class Professor {
           private int prof_id;
           private String name;
    ............... ....

    Code:
    Same with **PC.java**:
    
        public class PC {
          private int prof_id;
          private int course_id;
    ............... ...

    **
    Code:
    ArrayList<PC> results** contains prof_id <-> course_id
    **
    Code:
    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_cou rses" 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:

    Code:
       <%     
            //all the courses that a professor has
            String titles[] = CourseAssignments.getInstance().bridge1("John");
            %>
            
            <html>
               <head></head>
                 <body>
                 <center>
                    <table border="1" cellspacing="1" cellpadding="8" bgcolor= "33FFCC"> 
            			<tr>
            				<td bgcolor= #FF9966><b>Course</b></td>
            			</tr>
            			 	<%
            			    for (int i = 0; i < titles.length; i++){
            			    	if(titles[i] != null){
            				%>
            				<tr>
            				  <td><%= titles[i]%></td>
            				</tr>
            				<%
            	                 }
            	        	}
            %>
            			</table>
    I have Exception at the array:

    org.apache.jasp er.JasperExcept ion: java.lang.NullP ointerException
Working...