Home Answers Viewqa Java-Beginners Java null pointer exception

 
 


Michael
Java null pointer exception
2 Answer(s)      a year and 11 months ago
Posted in : Java Beginners

Hi I've made an array of Book objects that I made, the array is called library and it can hold 100 books, but currently there are 6. The Book objects contain three private strings; an author, a title and a short summary. A Book object also contains the appropriate constructor and getters and setters. Now I've made a method to search for a given string in a given field and it returns a string called results, but when I try run it, it gives me a NullPointerException. The code is shown below, please help:

protected static Book[] library=new Book[100];

public String searchLibrary(int field,String param){

    String result = "";
    String strfield = "";
    int counter=0;

    switch(field){
    case 1: strfield=library[counter].getAuthor();
    break;
    case 2: strfield=library[counter].getTitle();
    break;
    case 3: strfield=library[counter].getDdc();
    break;
    }

    while(library[counter]!=null){
        if(strfield.contains(param)){
            result+=library[counter].toString()+"\n";
        }
        counter++;
    }
    return result;
}
View Answers

June 16, 2011 at 5:03 PM


Ah I just figured out my error, sorry guys. What a simple mistake, I feel retarded.


June 16, 2011 at 5:58 PM


You can also try the following code:

import java.util.*;

 class Book{
    String author;
    String title;
    String summary;
    public Book(String author,String title,String summary){
    this.author=author;
    this.title=title;
    this.summary=summary;
    } 
    public String getTitle(){
        return title;
    }
    public String getAuthor(){
        return author;
    }
    public String getSummary(){
        return summary;
    }
}
public class BookInformation{
    public static void main(String[]args){
        Scanner input=new Scanner(System.in);
        Book myLibrary[]=new Book[6];
        for(int i=0;i<myLibrary.length;i++){
        System.out.print("Author: ");
        String author=input.nextLine();
        System.out.print("Title: ");
        String title=input.nextLine();
        System.out.print("Summary: ");
        String summary=input.nextLine();
        myLibrary[i]=new Book(author,title,summary);
        System.out.println();
        }
        System.out.println();
        System.out.println("Book Information");

        for (int i = 0; i < myLibrary.length; i++){
            Book library = myLibrary[i];
            System.out.println(library.getAuthor() + "\t "+ library.getTitle() + "\t "+ library.getSummary() + "\t ");
        }
        System.out.println();
        System.out.print("Enter title to search infomation: ");
        String tit=input.nextLine();
        System.out.println();
        for(int i = 0; i <  myLibrary.length; i++) {
            if(tit.equals(myLibrary[i].getTitle())) {
                System.out.println("Author is: "+myLibrary[i].getAuthor());
                System.out.println("Summary is: "+myLibrary[i].getSummary());
            }
        }
     }   
}









Related Pages:
null pointer exception in java
null pointer exception in java  What is null pointer exception in Java and when it occurs? Show it with an example.Thanks!   Java Null Pointer Exception
Null Pointer Exception
Null Pointer Exception  whis is the Null Pointer Exception in java? or define Null Pointer Exception with exp?   NullPointerException occurs, when you try to implement an application without referencing the object
Java null pointer exception
Java null pointer exception  Hi I've made an array of Book objects that I made, the array is called library and it can hold 100 books, but currently...]!=null){ if(strfield.contains(param)){ result+=library[counter
Null Pointer Exception - Java Beginners
Null Pointer Exception  How Can I Handle NullPoiniterException without using try/catch mechanism
Java null pointer exception handling
Java null pointer exception handling   What... program? Is this extends from exception class of Java?   Java null pointer exception handling What is Java NullPointerException? In Java
Httpservletresponse null pointer exception.
Httpservletresponse null pointer exception.  What is Httpservletresponse null pointer exception?   NullPointerException occurs when we try to implement an application without referencing the object and allocating
Null Pointer Exception
Null Pointer Exception        Null pointer exceptions are the most common run time .... Understand Null Pointer Exception Suppose we have a class Wide
Why the null Pointer exception is comming? - Java Beginners
Why the null Pointer exception is comming?  My code is given below...")); String []oldOne = null; String[]newOne; int i = 0...}; String line; String line1; String[] tokens = null
NUll POINTER EXCEPTION
of code i m getting null pointer Exception. can some one please explain what...NUll POINTER EXCEPTION  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
when do we get null pointer exception in java
when do we get null pointer exception in java  can anybody explain me abt null pointer exception? like in wt situation do v get NPE? Are these 2 strings same? String str; String str=null
Null pointer exception in hibernate - Hibernate
Null pointer exception in hibernate  Hi i am getting null pointer... is compiled successfully but while running the program i am getting null ponter exception. SessionFactory object is not creating pls give me the solution
check null exception java
check null exception java  check null exception java - How to check the null exception on Java?   The null pointer exception in java occurs... on it. See the example null pointer exception
null pointer exception cannot be removed
null pointer exception cannot be removed  class list { public... Exception nothing; public int r; public list() { this(null,null,0); } public list(String e,int i) { this.elem = e; this.next = null
Getting Null pointer Exception in the jsp page
Getting Null pointer Exception in the jsp page  I have developed this jsp page to store the data in database.While storing the value of check box in database I am getting null pointer exception in the following code
Getting Null pointer Exception in the jsp page
Getting Null pointer Exception in the jsp page  I have developed this jsp page to store the data in database.While storing the value of check box in database I am getting null pointer exception in the following code
Getting Null pointer Exception in the jsp page
Getting Null pointer Exception in the jsp page  I have developed this jsp page to store the data in database.While storing the value of check box in database I am getting null pointer exception in the following code
Getting Null pointer Exception in the jsp page
Getting Null pointer Exception in the jsp page  I have developed this jsp page to store the data in database.While storing the value of check box in database I am getting null pointer exception in the following code
null pointer exceptions
null pointer exceptions  import java.util.*; public class employee { static int ch; static int emp[]; String name; int id,age..."); } } } Exception in thread "main" java.lang.NullPointerException at employee.insert
Null pointer exception error in Jsp - JSP-Servlet
Null pointer exception error in Jsp  Hi i write a login page. when i validate the login value then the nullpointer exception error is occured. my..." attributes in the input tag So id,userpwd gives null pointer exception
null pointer error
null pointer error  Respected sir there is a problem in this code when i add any new record in ms access data base from my code, it gives only null pointer error sir please help me my code are given below compilatation class
null pointer error
null pointer error  Respected sir there is a problem in this code when i add any new record in ms access data base from my code, it gives only null pointer error sir please help me my code are given below compilatation class
Null pointer exception error in Jsp - JSP-Servlet
Null pointer exception error in Jsp  Expert:Majid Hi i write a login page. when i validate the login value then the nullpointer exception error..." attributes in the input tag So id,userpwd gives null pointer exception. Thanks
Handle Null Pointer Exception while text file upload
Handle Null Pointer Exception while text file upload  I want the code for handling null pointer exception in jsp page. That is I need an error page for null pointer exception. The cause for null pointer exception is while
null pointer
null pointer  dear sir , what is mean by null pointer in c
Clicking F5 and reentering value throws Null pointer exception
Clicking F5 and reentering value throws Null pointer exception  Hi, My page performs search operation.Normally when i enter a value and click... refresh my page and enter any value it is throwing Null pointer exception.can
pointer error java
pointer error java  How to catch null pointer errors in java if the if(value != null) or null
Null pointer exceptation-Java Servlet web application,Problem connecting with MYSQL database
Null pointer exceptation-Java Servlet web application,Problem connecting.... T_T I have develop my java web application in window Environment and now i.... T_T I have develop my java web application in window Environment and now i
Exception
in Java program: * If you are calling some method on the null object...Exception  what is NullPointerException?   Hi, I java... package. The NullPointerException is thrown by the Java program at the time
is there is pointer in java or not??
is there is pointer in java or not??  is there is pointer in java or not plz tell me this with brief?? thank you
Unhandled Exception
that not to be occurred Unhandled Exception are categorized into different type- 1)Java .lang. Null Pointer Exception-. A java.lang.NullPointer Exception is thrown... Unhandled Exception     
List of Java Exception
with the data type of that array Null Pointer Exception These Exception... List of Java Exception       Exception in Java are classified on the basis of 
List of Java Exception
with the data type of that array Null Pointer Exception... List of Java Exception     ...; the exception handled by the java compiler. Java consists of the following type
how to move pointer in Textfield to newline
how to move pointer in Textfield to newline  consider my case...); add(l4); add(tf1); add(tf2); add(tf3); setSize(400,600); setLayout(null...); setLayout(null); setVisible(true
Java Exception
causes a null to be passed to the constructor. Well in this case the exception...Java Exception  Explain about checked and unchecked Exceptions... this exception will be caught by a well-written application and will also prompt
Pointer in java - JSP-Servlet
Pointer in java   Hello ! All i know that java has no pointer type facility. but i have a problem to related pointer. Actually m using a dll and in that dll there is function that rec add of byte type variables address
exception
exception  chek in and check out exception in java   Please visit the following link: Checked and Unchecked Exception
Exception
Exception  whis is the Arithmetic Exception in java? or define Arithmetic Exception with exp?   Arithmetic Exception occurs, when you divide a number by zero. Example public class TryCatch { public static void main
exception
;Please visit the following links: http://www.roseindia.net/java/java-exception/user-defined-exception.shtml http://www.roseindia.net/java/exceptions/making
exception
exception  Identify the reason for SQLException exception, which..."); ps.executeUpdate(); } catch(Exception e...` ( `id` bigint(255) NOT NULL auto_increment
java.lang.NumberFormatException: null
java.lang.NumberFormatException: null   I get org.apache.jasper.JasperException: java.lang.NumberFormatException: null exception when i submit the form plz help me regarding
how to solve the nullPointer Exception in main - Java Beginners
how to solve the nullPointer Exception in main  New to Java ,pls help me how fix Null pointer Exception in main: two files , split .java another...*; public class split { public String[] spaceWord = null; public static void
Java error java.lang.nullpointerexception
to resolve Null Pointer Exception is to avoid performing the operation... on a object  or calling a method on the object i.e null. This exception is thrown... In this Tutorial we want to describe you a code that help you in understanding a Java Null
how to chech result set is null or not - Java Beginners
how to chech result set is null or not  Hi, really very thanks for fast reply.I want to know how to check whether the result set is null... { public static void main(String[] args) throws Exception { String driver
pointer
pointer  CONSTRTUCT A STRUCTURE FOR EMPLOYEE RECORD THAT CONTAINS A PERSON'S NAME, AGE, EMPLOYEE AND SALARY.WRITE A STATEMENT THAT DECLARES A VARIABLE empInfo and a pointer emPtr of type EMPLOYEE.then use the pointer to display
null interface
null interface   what is the null interface and what is the use of it in real java project ?   Hi Friend, A null interface is an interface without any methods.Is also known as Marker interface. Null interfaces are used
SQL Exception
but it can't be stores it shows an error about sql exception. blackberry.html <...;/body> </html> ** bb.jsp ------ ** <%@ page language="java..."); Connection con = null; PreparedStatement pstatement = null; int
Null Expression Example
Null Expression Example       In this section, you will see the example of null expression in with JPQL (Java Persistence Query Language). In this example, we
java runtime exception - JDBC
java runtime exception  while i executing this source code:- import...."); Connection con = null; String url = "jdbc:mysql://localhost:3306..."); } catch (Exception e) { e.printStackTrace
What is a pointer and does Java support pointers?
What is a pointer and does Java support pointers?   Hi, What is a pointer and does Java support pointers? Thanks   Hi, Pointer.... for more related to [Java Pointer][1] [1]http://www.roseindia.net
Regarding Exception - Java Beginners
= ""; return (text != null) && (!text.trim().equals(EMPTY_STRING)); } public

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.