Questions tagged [nullpointerexception]

The Java exception thrown when an application attempts to use null in a case where an object is required.

This Java is thrown when an application attempts to use null in a case where an object is required.

These include:

  • Calling the instance method of a null object.
  • Accessing or modifying the field of a null object.
  • Taking the length of null as if it were an array.
  • Accessing or modifying the slots of null as if it were an array.
  • Throwing null as if it were a Throwable value.
  • Unboxing of null object.

Applications should throw instances of this exception to indicate other illegal uses of the null object.

It's often abbreviated as NPE

Practically all questions with this tag are duplicates of the canonical What is a NullPointerException and how do I fix it?.

15961 questions
2
votes
2 answers

Null pointer exception in enhanced for loop over Vector

How could this code throw a null pointer exception? for (Foo f : Vector v) { f.doStuff(); // this line throws a NullPointerException } Even if the Vector is empty, shouldn't the inside block just never be executed?
wohanley
  • 434
  • 5
  • 14
2
votes
1 answer

Migrate from SharedPreferences to Jetpack DataStore "Java"

According to this question, I am trying to migrate the current project from SharedPreferences to the dataStore to store the value of layout chosen by the user, the problem is with reading the value, I got NPE, first this my code The inner…
2
votes
1 answer

unable to setText on TextView. Giving NullPointerException

I am setting text on textview but it's giving me NullPointerException Here's my XML file.
James Bond
  • 47
  • 12
2
votes
1 answer

Kotlin Mockito NullPointerException

There are classes: @Singleton class Exchange( @Client("\${exchange.rest.url}") @Inject val httpClient: RxHttpClient ) : Exchange { override suspend fun getSymbols(): List { val response =…
2
votes
1 answer

Cannot invoke "com.microsoft.sqlserver.jdbc.TDSReader.peekTokenType()" because "tdsReader" is null

I am trying to connect to an online database. Every now and then it gives me the following error: Exception in thread "Timer" java.lang.NullPointerException: Cannot invoke "com.microsoft.sqlserver.jdbc.TDSReader.peekTokenType()" because "tdsReader"…
NiNoes
  • 37
  • 6
2
votes
3 answers

Annoying NullPointerException

Here is my main class: import android.graphics.Rect; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.widget.RelativeLayout; public final class HomeScreen implements OnTouchListener { public…
gsfd
  • 1,070
  • 2
  • 12
  • 17
2
votes
3 answers

Cannot throw custom exception using orElseThrow in Java

I am trying to throw a custom exception using ternary operation and orElseThrow as shown below: public static MainProviderType getMainProviderType(ProviderType providerType) { return…
2
votes
2 answers

Groovy give NPE on list.find call but only after a period of time

We have a chunk of code something like this // semi-pseudo code def result = someList.find { condition == true } (someList may be null, but that is ok in groovy as null.find{…} works fine.) This line of code is running in an action of a grails…
k s
  • 882
  • 8
  • 9
2
votes
3 answers

Map.ofEntries gives Null Pointer Exception on checking NULL key using containsKey()

I was fetching key from a constant map earlier using HashMap. On passing a NULL key at containsKey(), I used to get FALSE. To make the code look fancy, I tried java-8 over it. So, instead of HashMap, I started using Map.ofEntries to build my…
Aditya Rewari
  • 2,343
  • 2
  • 23
  • 36
2
votes
2 answers

java.lang.NullPointerException at io.dropwizard.testing.junit5.DropwizardExtensionsSupport.beforeEach

I am trying to test a Dropwizard resource. My test looks like this: @ExtendWith(DropwizardExtensionsSupport.class) public class CommonObjectsTest { private ResourceExtension EXT; @BeforeEach public void setup() { …
umop apisdn
  • 653
  • 9
  • 20
2
votes
1 answer

Kotlin Spring Data JPA - Declared non-null type is NULL

Using Kotlin, Spring Data JPA with Hibernate. Consider following JPA entity: @Entity @Table(name = "applications") class Application( @Column(name = "name", nullable = false) var name: String, @Column(name = "version", nullable =…
jantobola
  • 688
  • 2
  • 10
  • 28
2
votes
1 answer

BlockingQueue loses its reference and throws NullPointerException onMessage Jetty WebSocket

Had to ask this question because it's been a day trying to solve the problem and cannot. I'm working with Netbeans 8.2 and java 8. Topology: WebSocket client on the browser Jetty WebSocket server (java app with swing GUI) Objective: to send data…
Andrew
  • 63
  • 6
2
votes
2 answers

Android NullPointerException while copying exif tags

I am trying to re-size an image, that part completed. Then I'm trying to copy exif tags to new file. I use ExifInterface to read tags. I know it's an interface not an object. But When I try to use it for really big sized image, I get…
Vikas
  • 24,082
  • 37
  • 117
  • 159
2
votes
2 answers

How to avoid redundant try-catch statements when there might be null pointers?

I m experimenting with "new" libraries i found online by taking a close look at a covid tracing app. I scan a QR code, decode base64 to get the shop data in a Json string. I then use Gson to parse into a Shop POJO. public class Shop { public…
2
votes
1 answer

Send byte array from web service to client

I want to send a byte array from a web service to a client that requests an operation exposed via the service. In my method, I read an image into a byte array. I think place this byte array into a wrapper POJO. This is the return type for the…
Joeblackdev
  • 7,217
  • 24
  • 69
  • 106
1 2 3
99
100