Generics

Generics

public class A {
  int i;
  int j;
  void funA()
  {
    i = i+1;
    System.out.println(i);
    j = j+1;
    System.out.println(j);
  }
}

......................................................................

public class Demo<G> {
  G x;
  public static void main(String[] args) {

  Demo<A> d3 = new Demo<A>();
  try
  {
    d3.x.i = 5;//why Null pointer Exception
  }
  catch (Exception e) {
    e.printStackTrace();
  }
 }
}
View Answers

December 31, 2012 at 3:20 PM

Hi jagan,

Before you have assign the value for i, you should declare x object explicitly with new operator d3.x = new A(); d3.x.i = 5; Now null pointer exception won't come.

public class Demo<G> {
  G x;
  public static void main(String[] args) {

  Demo<A> d3 = new Demo<A>();
  try
  {
    d3.x = new A();
    d3.x.i = 5;//why Null pointer Exception
  }
  catch (Exception e) {
    e.printStackTrace();
  }
 }
}

Please try with this code.









Related Tutorials/Questions & Answers:
Generics
Generics
Advertisements
Generics
Generics
Generics
Generics
Generics
Generics
Generics
Generics
Generics
Generics
Generics
Generics
Generics
Generics
Generics
Generics
Generics
Generics
Generics
Generics
Generics
Generics
Generics
Generics
Generics
Generics
Generics
eraser type generics in java
eraser type generics in java  Explain eraser type generics in java with example
Why Generics was introduced in Java?
Why Generics was introduced in Java?  Hi, Why Generics was introduced in Java and what is was its motive? Thanks   Generics were... for compile time type checking. Read the tutorial for more details of Generics
generics - Java Beginners
generics  simple notes on Generics as well some programmes, with its... this situation Generics provides a way to communicate the collection type.... Generics provide a mechanism for annotating type information for the use
Java Generics
.style1 { list-style-type: square; } Java Generics The Generics.... In general words, Generics allows us to create a single method for sorting... defined types. Generics permits us to catch the incompatible types during
Introduction to Generics in Java
Introduction to Generics in Java: What are the benefits of using Generics in Java? The Generics Features was added to the Java with the release of Java 5.0. The Generics was the long-waited enhancement which is added to the Java 5.0.
Wildcards in Generics
Wildcards in Generics In this section, you will learn the use wildcards or wildcard character in Generics. Problem : For better understanding of wildcards..., this is assignment is not possible in Generics. Solution : Now, the solution of above
When were generics first introduced in Java?
When were generics first introduced in Java?  Hi, I want to know when were generics first introduced in java?   Generics were introduced... the tutorial: Introduction to Generics in Java. Thanks
Generics Example Program in Java
Generics Example Program in Java explaining how you use it effectively? In this section we will explain you how you can use the Generics while writing the Java Program? The introduction to the Generics is the major step in the Java
Generics Method in Java
Generics Method in Java       After going through the example, you will be able to declare and use generic methods in Java programming language. As you already know, generic
Java Generic Iterator
Java Generic Iterator is the new feature added to the jdk1.5. It is used to generalize the Collection classes. It makes the Collection class type safe. It is used with classes, methods and interfaces. Java Generics Iterator
Java arraylist generics example and arraylist generic list
Generics is used to make  the data type safe program. It is the new feature of java 5. When the ArrayList is generalized for a specific data type, other data types cannot be added. It gives error on using the other
Java generic
to the Java Language including "Java Generics". Using generics makes...? framework is one of the places where Generics are most used. Until the introduction of Generics, when you take an element out of a collection they required
Java Glossary Term - G
Java Glossary Term - G       Java Generic Java 1.5 provides number of new features to the Java Language including "Java Generics". Using generics makes
code - Java Beginners
code  Example on exception handling with generics
Advanced Java
Advanced Java   What is the difference between byte code generated between that of Legacy collections and Generics
java - Java Interview Questions
java  What is the purpose of Generics in collection?   Hi I am sending a link where you can find details about generics/collection...;Generics provides a way for you to communicate the type of a collection
JDK 1.5 Features Diff - Java Interview Questions
JDK 1.5 Features Diff   Hi Frieds , Can u explain me d difference bn Generics and Autoboxing in jdk 1.5.  Hi Generics?Provides compile-time type safety for collections
help needed - Java Interview Questions
help needed  What are the new features added in jdk 1.5?  Hi friend, The following were the new features added in java 1.5. * Generics * Enhanced for loop * Metadata * Static import * Typesafe enums
Java - Java Interview Questions
Java  How to implement Generics Concept in programs?  Hi When you take an element out of a Collection, you must cast it to the type...(); ) if (((String) i.next()).length() == 4) i.remove(); } Generics
java - Java Interview Questions
framework better. They also added more features such as Generics, Annotations any many more. Generics made the collection framework to store the objects
compilation error - Java Beginners
generics or you are using deprecated methods. IF you want to check which lines

Ads