getClass().getName() returns null

getClass().getName() returns null

By : user2953204
Date : November 20 2020, 01:01 AM
I wish did fix the issue. Skip the lazy initialization in initName! It is not thread-safe.
Instead, initialize the ThreadLocal field directly (and also note the final):
code :
private static final ThreadLocal<String> className = new ThreadLocal<>();

Share : facebook icon twitter icon
Java getClass().getName() it returns the class not the name

Java getClass().getName() it returns the class not the name

By : Mohita Sharma
Date : March 29 2020, 07:55 AM
will be helpful for those in need getClass() returns the Class object representing the "Card" class; therefore the code does not refer to Card#getName method, but rather to Class#getName which dutifully returns "Card".
Simply remove getClass():
code :
public String toString()
    return getName();
nullPointerException, guess when using getClass().getName()

nullPointerException, guess when using getClass().getName()

By : Sumit
Date : March 29 2020, 07:55 AM
it helps some times If your comment about where the exception occurs is correct, then method is null. You should verify this by using a debugger to step to that piece of code and inspect the statement. Alternatively, add Sysouts for all involved variables.
If this is unexpected, say you're convinced that methodis set by some other part of your code, add a breakpoint or output to that other part to verify that it's actually doing what you think it's doing, and in the correct order, too.
In Java, assuming we have a class called Foo, how does obj.getClass().getName() and Foo.class.getName() fetch same resul

In Java, assuming we have a class called Foo, how does obj.getClass().getName() and Foo.class.getName() fetch same resul

By : futureorbit
Date : March 29 2020, 07:55 AM
To fix this issue Foo.class is a static instance of type Class; there is only one such instance at runtime.*
If obj is of type Foo, then obj.getClass() returns Foo.class, by definition.
How to do object.getClass().getName() in Java

How to do object.getClass().getName() in Java

By : S.jana
Date : March 29 2020, 07:55 AM
wish help you to fix your issue Looks like you are practicing some OO with Java . It's better if you ask a doubt instead the full scenario.
However I will paste a sample here and you can ask any doubt you have. FoodFactory class below, note that for a name you will get the right Object.
code :
public class FoodFactory {

  public Food getFood(String food) {

    if ("Meat".equals(food)) {
      return new Meat();
    } else if ("Fruit".equals(food)) {
      return new Fruit();
    throw new IllegalArgumentException("No food found with name " + food);
public class Meat extends Food {
public class Food {
  public void serveFood() {
    System.out.println("I'm serving " + getClass().getSimpleName());
FoodFactory ff = new FoodFactory();
Food f1 = ff.getFood("Fruit");
Food f2 = ff.getFood("Meat");
Using getClass().getName() in toString() methods

Using getClass().getName() in toString() methods

By : bored_panda
Date : March 29 2020, 07:55 AM
around this issue getClass() is a polymorphic method. It returns the actual concrete class of the object on which it is called. Since d id of type DerivedClass, its getClass() method returns DerivedClass.class.
You would get the result you expect if the method implementation was
Related Posts Related Posts :
  • Multiple Consumers Spring Kafka
  • Bitbucket pipeline: environmental variables in build.gradle file not recognized
  • Java static enum method to return a default enum value
  • What is the complexity of empty for loop?
  • Group randomly List of Lists as N subsets with K elements in each
  • How to set texture on a shape drawn by glDrawArrays()?
  • How to make a jlabel resizable according to the window
  • Java Mathematical Expression Syntax
  • assigning values to characters in java
  • Method overriding and Inheritance in java
  • given a set of lists/groups with a series of number, find matching numbers
  • Connection to HID USB device (keyboard and mouse) in android
  • Inject HttpServletRequest in CDI SessionScoped bean
  • Pool game in java - ball collision algorithm
  • Focus ScrollView to selected position programmatically - Android
  • Is it hibernate bug?
  • Glib memory allocation error
  • Android Proguard - step by step
  • Setting the pivot point of a JPanel to its center
  • java check time is greater time
  • how to save a screenshot (matlab)
  • Anti-aliasing filled shapes in libgdx
  • Gwt custom text box having baloon popup
  • How to change date dynamically for each element in an Array
  • JPanel won't add the JLabel text?
  • how to access the .properties file in ant if it is in different location
  • Reduce application memory footprint
  • java.lang.ClassCastException: javax.mail.Session cannot be cast to javax.mail.Session
  • What is the exact purpose of calling System.exit() in java
  • How to do you get output from Javascript into Java using ScriptEngine
  • Java replaceAll() method to escape special characters
  • Java (Removing a 'keyword' from the alphabet)
  • Format BigDecimal in Spring
  • iterating checkbox values stored in array using EL
  • Java - Jackcess API with .accde(MS Access) format
  • Alternative for some of Struts 1 methods in Struts 2
  • javax.crypto JDK source code, again
  • Spring Roo - Command 'service' not found
  • antlr4: ATN version 2 expected 3
  • Deploy GlassFish using Netbeans generate java.net.MalformedURLException: Bad URL path
  • why is wait() called for loop in Thread.join() of Java?
  • Java JAXB marshall into DOM Document
  • Why JDBI 3 @ColumnName annotation doesn't work?
  • Java SimpleDateformatter with 10 decimals after the seconds, cannot convert to Date
  • Getting array index out of bounds when splitting a string in android
  • Verify if csv file is updated successfully
  • Get the parameter names of an interface method
  • How to retrieve object state from serialized Java objects without class file(s)
  • How to move a programmed game for pc to android
  • Size of an object and a string in Java
  • PrimeFaces Datatable Right Click Select + Row Specific Context Menu
  • How to use OOP to branch from a point class into lines and triangles (Java)
  • What does 'String...' mean?
  • Add YouTube Data API to Android Studio
  • JComboBox in JTable not displaying options, only the correct number of blank options
  • JTable and removeAll behavior
  • Google App Engine: Can I programmatically find out if this is the default version?
  • How do you open a program by using ActionListener in Java?
  • Android SipManager: android.net.sip.SipException: SipService.createSession() returns null
  • Forcefully terminating a thread I didn't write in Java
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org