🚀 Java programming language | Buggy code🤯
Welcome to your programming channel 👋🏻. Today we are talking about the buggy code in Java. So if you want to know more about this topic, stay with me until the end of this education.
1.BufferOverflowError: Array length too small
public class BufferOverflowErrorExample {
public static void main(String[] args) {
int[] intArray = new int[5];
for (int i = 0; i <= intArray.length; i++) {
intArray[i] = i * 2;
}
}
}
This code will cause a BufferOverflowError
because the array index (i
) goes out of bounds when it reaches the value of intArray.length
.
2.ArrayIndexOutOfBoundsException: Array length too small
public class ArrayIndexOutOfBoundsExceptionExample {
public static void main(String[] args) {
int[] intArray = new int[5];
for (int i = 0; i < intArray.length + 1; i++) {
intArray[i] = i * 2;
}
}
}
This code will throw an ArrayIndexOutOfBoundsException
because the array index (i
) goes out of bounds when it reaches the value of intArray.length + 1
.
3.NullPointerException: Null array reference
public class NullPointerExceptionExample {
public static void main(String[] args) {
int[] intArray = null;
for (int i = 0; i < intArray.length; i++) {
intArray[i] = i * 2;
}
}
}
This code will throw a NullPointerException
because it tries to access a method (length
) of a null array reference (intArray
).
4.ConcurrentModificationException: Iterator.remove()
import java.util.ArrayList;
import java.util.Iterator;
public class ConcurrentModificationExceptionExample {
public static void main(String[] args) {
ArrayList<Integer> intList = new ArrayList<>();
intList.add(1);
intList.add(2);
intList.add(3);
intList.add(4);
intList.add(5);
Iterator<Integer> iterator = intList.iterator();
while (iterator.hasNext()) {
iterator.next();
iterator.remove();
}
}
}
This code will throw a ConcurrentModificationException
because it attempts to remove elements from the list while iterating over it.
5.NoSuchMethodError: Undefined method
public class NoSuchMethodErrorExample {
public static void main(String[] args) {
int[] intArray = new int[5];
intArray.sort(); // No such method in int[]. This method is present in List.
}
}
This code will throw a NoSuchMethodError
because it calls the sort()
method on an array (intArray
). The sort()
method is undefined for arrays and is available only for List
s.
6.OutOfMemoryError: Exhausted heap memory
public class OutOfMemoryErrorExample {
public static void main(String[] args) {
ArrayList<String> stringList = new ArrayList<>();
String testString = "Test string";
while (true) {
stringList.add(testString);
}
}
}
This code will throw an OutOfMemoryError
because it keeps adding strings to the list (stringList
) without ever freeing up memory. The Java heap space will become full, causing the OutOfMemoryError
.
7.ClassNotFoundException: Undefined class
public class ClassNotFoundExceptionExample {
public static void main(String[] args) {
try {
Class<?> cls = Class.forName("com.example.UndefinedClass");
} catch (ClassNotFoundException e) {
System.out.println("ClassNotFoundException: " + e.getMessage());
}
}
}
This code will throw a ClassNotFoundException
because it tries to load a class (com.example.UndefinedClass
) that has not been defined.
8.UnsupportedOperationException: Unsupported operation
import java.util.Collections;
import java.util.List;
public class UnsupportedOperationExceptionExample {
public static void main(String[] args) {
List<String> unmodifiableList = Collections.unmodifiableList(new ArrayList<>());
unmodifiableList.add("Test string"); // Unsupported operation
}
}
This code will throw an UnsupportedOperationException
because it tries to add a string to an unmodifiable list (unmodifiableList
). Unmodifiable lists do not support addition, deletion, or modification operations.
9.NumberFormatException: Invalid format for integer parsing
public class NumberFormatExceptionExample {
public static void main(String[] args) {
String invalidNumber = "1234a";
try {
int intNumber = Integer.parseInt(invalidNumber);
} catch (NumberFormatException e) {
System.out.println("NumberFormatException: " + e.getMessage());
}
}
}
This code will throw a NumberFormatException
because it tries to parse an invalid number (1234a
) as an integer. The Integer.parseInt()
method can only parse strings that represent valid integers.
10.IOException: Error while reading/writing from/to a file
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class IOExceptionExample {
public static void main(String[] args) {
try {
FileInputStream inputStream = new FileInputStream("input.txt");
FileOutputStream outputStream = new FileOutputStream("output.txt");
int data;
while ((data = inputStream.read()) != -1) {
outputStream.write(data);
}
inputStream.close();
outputStream.close();
} catch (IOException e) {
System.out.println("IOException: " + e.getMessage());
}
}
}
This code will throw an IOException
if an error occurs while reading from the file (input.txt
) or writing to the file (output.txt
). Common reasons for this exception include file not found, insufficient permissions, or disk full.