The NoSuchElementException exception occurs when accessing an element that doesn’t exist. Find out the best practices of avoiding it and writing robust code.
Also find me here:
Baeldung Author
Abderrahim Azhrioun
Full Stack Developer, Java Geek, Spring Lover, and a Blogger. I believe coding should be fun and accessible to everyone ;)
Here's what I've written (so far):
Baeldung on Java
- All
- Java String (7)
- Java Dates (5)
- Java (4)
- Spring Boot (3)
- Java IO (3)
- Spring Web (2)
- Spring MVC (2)
- Spring Data (2)
- Jackson (2)
- Spring Persistence (1)
- Spring (1)
- REST (1)
- Programming (1)
- Persistence (1)
- Java Streams (1)
- Java Map (1)
- Java Array (1)
- JSON (1)
- HTTP Client-Side (1)
Convert File to Byte Array in Java
Filed under Java IO
Learn how to convert a file into a byte array in Java.
Get First Date of Current Month in Java
Filed under Java Dates
Learn how to obtain the first date of the current month in Java using JDK classes and third-party libraries like Joda Time.
Check if a String Contains a Number Value in Java
Filed under Java String
Learn how to check if a string contains a number in Java.
Capitalize the First Letter of Each Word in a String
Filed under Java String
Learn how to capitalize the initial character of each word in a Java string.
Convert Char Array to Int Array in Java
Filed under Java Array
Explore how to convert a char array to an int array in Java.
Converting HashMap Values to an ArrayList in Java
Filed under Java Map
Explore several ways to convert a HashMap to an ArrayList in Java.
Getting Yesterday’s Date in Java
Filed under Java Dates
Learn how to easily get yesterday’s date in Java.
How to Get Last Day of a Month in Java
Filed under Java Dates
Discover how to find the last day of a month in Java using both core Java methods and popular libraries. This article provides step-by-step explanations for accomplishing the task using Date and Calendar classes, Java 8’s Date Time API, and the Joda Time library with code examples and test cases.
Adding One Month to Current Date in Java
Filed under Java Dates
In this short tutorial, we discuss various methods of adding one month to the current date in Java. We cover using core Java methods as well as popular external libraries such as Joda-Time and Apache Commons Lang3.
Difference Between findBy and findOneBy in Spring Data JPA
Filed under Spring Data
This article delves into the similarities and differences between the findBy and findOneBy prefixes in Spring Data JPA. It explores the concept of derived query methods, provides code examples, and explains the exception thrown by findBy queries when the result size doesn’t match expectations.
Handle NoSuchElementException When Reading a File Through Scanner
Filed under Programming
In this tutorial, we’ll explore handling the ‘NoSuchElementException: No line found’ exception when reading a file using the Scanner class in Java. We’ll explain its root cause and present three solutions: defensive programming, exception handling, and checking file emptiness.
What’s the Difference between Scanner next() and nextLine() Methods?
Filed under Java
This article delves into the difference between the next() and nextLine() methods of the Scanner class.The article also covers the use of custom delimiters and provides code examples for better understanding. Understanding these distinctions enables developers to parse input effectively using Scanner.
Java PrintStream to String
Filed under Java IO, Java String
This article provides several ways of converting a PrintStream to a String in Java.The approaches include using ByteArrayOutputStream, a custom output stream, and Apache Commons IO.
Spring Boot H2 JdbcSQLSyntaxErrorException expected “identifier”
Filed under Persistence, Spring Boot
This tutorial explains the exception org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement expected “identifier”. It provides a practical example to reproduce the exception and showcases how to fix it by double-quoting the keyword or using a JDBC URL workaround. It also covers how to automatically quote all database identifiers when using Hibernate.
Check if the First Letter of a String Is a Number
Filed under Java String
This tutorial explains different ways to check if the first character of a string is a number in Java. It covers using charAt(), Character.isDigit(), Pattern class, and matches() method. It also demonstrates how to use Guava’s CharMatcher utility class to accomplish the same objective.
Fix the JsonMappingException: Can not deserialize instance of java.util.ArrayList from Object value (token `JsonToken.START_OBJECT`)
Filed under Jackson
Learn how to fix a common JsonMappingException when deserializing a JSON string.
Fix Spring Data JPA Exception: No Property Found for Type
Filed under Spring Data
This article explains how to fix the “PropertyReferenceException: No property found for type” in Spring Data JPA. It advises using exact property names when defining query methods to avoid the exception. Practical examples are provided.
Fix EmptyResultDataAccessException When Using JdbcTemplate
Filed under Spring Persistence
Explanation of the reason behind EmptyResultDataAccessException when jdbcTemplate is used and how to fix it.
Fix the IllegalArgumentException: No enum const class
Filed under Java
Why IllegalArgumentException is thrown and how to fix in it in Java.
Convert Anonymous Class into Lambda in Java
Filed under Java
A reminder of what an anonymous class is and how to convert it to a lambda expression.
Enum Mapping in Spring Boot
Filed under Spring
Explore different ways to implement case-insensitive enum mapping in Spring Boot.
Compare Characters in Java
Filed under Java
Learn how to compare characters in Java.
How to Truncate a String in Java
Filed under Java String
Learn how to truncate a String in Java.
Format Instant to String in Java
Filed under Java Dates
Learn two different ways to format an instant as a string – one using core Java and the other using the Joda-Time library.
HttpMessageNotWritableException: No Converter for [class …] With Preset Content-Type
Filed under Spring Web
Learn what causes Spring to throw HttpMessageNotWritableException with the message “No converter for [class …] with preset Content-Type” and how to solve it.
Check if a String Ends with a Certain Pattern in Java
Filed under Java String
Learn how to check if a String ends in a certain pattern in Java using core Java methods as well as Apache Commons Lang’s StringUtils class.
Split a String Every n Characters in Java
Filed under Java String
Learn several ways to split a string after every n characters in Java.
Spring Boot Error ApplicationContextException
Filed under Spring Boot
Learn how to solve the ApplicationContextException in Spring Boot.
“HttpMessageNotWritableException: No converter found for return value of type”
Filed under Jackson, REST, Spring MVC
Learn about Spring’s HttpMessageNotWritableException: “No converter found for return value of type” exception.
Spring RestTemplate Exception: “Not enough variables available to expand”
Filed under HTTP Client-Side, Spring Web
Learn about Spring’s RestTemplate Not enough variables available to expand IllegalArgumentException.
Find the Last Modified File in a Directory with Java
Filed under Java IO
Learn about file access management in Java
JSON Parameters with Spring MVC
Filed under JSON, Spring MVC
Learn how to send JSON parameters in GET and POST requests when using Spring MVC.
The Spring @ConditionalOnProperty Annotation
Filed under Spring Boot
Learn all about the Spring @ConditionalOnProperty annotation.