
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Can we insert null values in a Java list?
Solution
Yes, We can insert null values to a list easily using its add() method. In case of List implementation does not support null then it will throw NullPointerException.
Syntax
boolean add(E e)
Appends the specified element to the end of this list.
Type Parameter
E − The runtime type of the element.
Parameters
e − Element to be appended to this list
Returns
It returns true.
Throws
UnsupportedOperationException − If the add operation is not supported by this list
ClassCastException − If the class of the specified element prevents it from being added to this list
NullPointerException − If the specified element is null and this list does not permit null elements
IllegalArgumentException − If some property of this element prevents it from being added to this list
Example
The following example shows how to insert null values to the list using add() method.
package com.tutorialspoint; import java.util.ArrayList; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { // Create a list object List<String> list = new ArrayList<>(); // add elements to the list list.add("A"); list.add(null); list.add("B"); list.add(null); list.add("C"); // print the list System.out.println(list); } }
Output
This will produce the following result −
[A, null, B, null, C]
- Related Articles
- Can we insert records in a MySQL table without auto_increment values?
- Can we add null elements to a Set in Java?
- How can we specify default values in MySQL INSERT statement?
- Can we insert values without mentioning the column name in MySQL?
- How can we insert multiple tabs into a single JTabbedPane in Java?
- How can we add/insert a JButton to JTable cell in Java?
- Can we sort a list with Lambda in Java?
- How can we add/insert a JCheckBox inside a JTable cell in Java?
- Can we convert a list to a Set in Java?
- Can we convert a Java array to list?
- Can we convert a Java list to array?
- How can we handle NULL values stored in a MySQL table by using PHP script?
- How can we add/insert a JRadioButton to a JTable cell in Java?\n
- Can we convert a list to an Array in Java?
- How to set NULL values in a table column and insert the same
