Is AppCompatActivity object required for calling setSupportActionBar() method? - java

I went through a code where setSupportActionBar(toolbar) method was used without object of AppCompatActivity class. But when I went on implementing some code, I found it rejecting the way. It is important to use it as the toolbar I am passing as parameter to the above mentioned function is of type androidx.appcompat.widget.toolbar.
After implementing it, there is no compile time error, but facing error when starting the emulator:
"Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'void
androidx.appcompat.app.AppCompatActivity.setSupportActionBar(androidx.appcompat.widget.Toolbar)'
on a null object reference'.

Related

NullPointerException: 'void android.widget.Button.setText(java.lang.CharSequence)' on a null object reference [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 1 year ago.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setText(java.lang.CharSequence)' on a null object reference
at com.example.mentalhealth.diary.onCreate(diary.java:49)
In your diary.java, #line no:49, in the onCreate() method, it appears that you are using an object which is not instantiated.
Calling a method on that object (which is just declared , but not instantiated using new classname() syntax).
if your file is shared , at least the 49th line of your sourcecode, it would be easy to correctly mention .

Calling set on VarHandle in java 14 causes UnsatisfiedLinkError

I'm invoking the set method via reflection on the VarHandle instance
set.invoke(varHandle, arrayOf(field, field.modifiers and Modifier.FINAL.inv()))
However I'm getting UnsatisfiedLinkError everytime and I wasn't able to find anything related to my situation.
Caused by: java.lang.UnsatisfiedLinkError: 'void java.lang.invoke.VarHandle.set(java.lang.Object[])'
The field VarHandle points to is the modifiers field of Field class

How a method can be called without object reference

I am learning android and I am newbie. I encountered a code i.e.
FramgmentManager fm = getFragmentManager();
I know that getFragmentManager() is a method defined in Activity class.
In java we access method through object reference that is obj.method() but here how can we call getFragmentMananger without any object refrence
In your case there is an 'automatic' this reference meaning that your method is called on the enclosing object you're using right now (an Activity).

How Console reference variable refers to some other class method

I was strucked in a place that i cannot create new instance for Console class. So i took the source code of jdk and then look into it. Then i got cleared that it was declared as "public final class Console........"... So i understood that the final class cannot be instantiated. But that is not my actual doubt. Here in the SCJP Book, i came across a line of code that tells me as Console c = System.console()
Here we cannot create new instance for console class, so creating a reference variable c. Then What is that System.console actually is?? In the book it is told as
Keep in mind that it's possible for your Java program to be running in an environment that doesn't have access to a console object, so be sure that your invocation of System.console() actually returns a valid console reference and not null.
So then i entered to look up source code for System.console().
There i happen to see System as final class and console() as static method inside that.
So how can a Console reference object refer to that console method in system class..
What is the link between these two. I thought of a polymorphic reference.
But that is not because it doesn't pass IS-A Test. So please explain me in detail about this.
Hope my question is clear. Thanks in advance. !
the final class cannot be instantiated
That is wrong , final class cannot be subclassed. It can be instantiated using the new operator if its constructor is visible . I guess Console class has private constructor. Abstract classes cannot be instantited. See the JLS 8.1.1.2:
A class can be declared final if its definition is complete and no subclasses are desired or required.
It is a compile-time error if the name of a final class appears in the extends clause (ยง8.1.4) of another class declaration; this implies that a final class cannot have any subclasses.
System.console()
Returns the unique Console object associated with the current Java virtual machine, if any.
Whether a virtual machine has a console is dependent upon the underlying platform and also upon the manner in which the virtual machine is invoked. If the virtual machine is started from an interactive command line without redirecting the standard input and output streams then its console will exist and will typically be connected to the keyboard and display from which the virtual machine was launched. If the virtual machine is started automatically, for example by a background job scheduler, then it will typically not have a console.
If this virtual machine has a console then it is represented by a unique instance of this class which can be obtained by invoking the System.console() method.
If no console device is available then an invocation of that method will return null.
console() is some sort of a factory method which gives you back an object of Console associated with the current JVM . The System class has knowledge of the JVM in which it is running and it is the perfect candidate to give you back the Console object.
Console c = System.console();
System.console() gives us back a reference to the Console object and you are assigning the object reference to variable c. This way you can use that reference variable c to access the properties or methods of Console object.
I believe the mistake in your understanding is here:
So i understood that the final class cannot be instantiated
Final classes cannot be extended but can be instantiated. FYI it is the abstract class than cannot be instantiated.

launch(ProjectTaskManagementApp.class, args); ERROR

With the following code:
launch(ProjectTaskManagementApp.class, args);
There is the error:
The method launch(Class, String[]) is undefined for the
type ProjectTaskManagementApp
Why is it doing this? Could anyone help resolve the issue.
Thanks!
It's doing that because there's no method launch that takes a Class object and an array of String as arguments in the class ProjectTaskManagementApp. The method you're thinking of may have a different name, or may take different arguments, or it may simply not be there.
Based on the fact that you're calling launch without an object reference, it looks like this call is actually inside the definition of the class ProjectTaskManagementApp. Do you mean to be calling a launch method defined in this class, or it it defined in some other class? Do you mean to be calling launch via an object?
Simply put, the class: ProjectTaskManagementApp does not contain the method with the exact parameters you are calling it with, or does not exist at all.

Categories