Google Firebase real time database updateChildren() method is not working? [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
In my application, I am using google firebase real-time database with admin-sdk for Java. When I run a sample program from my machine it works perfectly and updates the given data into firebase db. But, when I try to run the same program from another public server, it's not working. It doesn't throw any exceptions at all. updateChildren() was executed, and then nothing. I tried with onCompletionListener too.There is nothing in there. What might be the problem?
FBManager.java
import com.nexge.firebase.Firebase;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import java.util.HashMap;
class FBManager {
//FIREBASE
public static Firebase fireBase;
public static DatabaseReference dbRef = null;
public static String filePath="key/siva-6d0aa-firebase-adminsdk-1bo0t-e98c5af5d5.json";
public static String databaseUrl="https://siva-6d0aa.firebaseio.com";
public static String projectName="ABES";
public static String databaseChildPath;
public static void main(String[] args) {
try {
databaseChildPath="calls/";
System.out.println("Inside iniDB !");
fireBase = new Firebase(filePath, databaseUrl, projectName, databaseChildPath);
System.out.println("FireBase db created!");
dbRef = fireBase.getDatabaseReference();
System.out.println("Got reference!");
T t = new T();
new Thread(t).start();
}
catch(Exception e) {
e.printStackTrace();
}
}
}
T.java
class T implements Runnable {
public void run() {
try {
String path = "191963944/5e0e44e2-3442-4acd-977a-fea7232c0f0a";
HashMap hm1 = new HashMap();
hm1.put("caller_Recv_Media_IpPort","xxx.xxx.xx.xxx:9811");
HashMap hm2 = new HashMap();
hm2.put("yServer_src_Media_IpPort","xxx.xx.xx.xx:10000");
FBManager.dbRef.child(path).updateChildren(hm1);
FBManager.dbRef.child(path).updateChildren(hm2);
Thread.sleep(10000);
}
catch(Exception e) {
e.printStackTrace();
}
}
}

It seems this is to do with server network issue. Working fine in another server. Closing this question. thanks

Related

Calling Java class in another class not working for me [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 months ago.
Improve this question
I thought when Java classes are in the same directory, you don't need to import another class when you use it inside another.
I have this class that will initiate Cloudinary uploading of files back to the cloud, but when I call it inside another class and run build I get this error 'cannot access CloudinaryUpload'
The Cloudinary class
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.cloudinary.Cloudinary;
import com.cloudinary.utils.ObjectUtils;
public class CloudinaryUpload {
public static void main(String arg[])throws Exception{
Map config = ObjectUtils.asMap(
"cloud_name", "name",
"api_key", "api_key",
"api_secret", "api_secret",
"secure", true
);
Cloudinary cloudinary = new Cloudinary(config);
}
}
Just a snippet of code of another class where I am calling it
public class ClientManagerServices {
private static final int BYTES_DOWNLOAD = 1024;
//The Cloudinary class
private CloudinaryUpload cloudinaryUpload = CloudinaryUpload();
public static String getMessageBody(Delegator delegator, String requester, String subject, String registryFileId, String clientId) {
GenericValue fileData = null;
GenericValue userData = null;
GenericValue clientData = null;
String bodyToReturn = "";
try {
fileData = delegator.findOne("RegistryFile", UtilMisc.toMap("registryFileId", registryFileId), false);
} catch (GenericEntityException e) {
e.printStackTrace();
}
}
This line:
private CloudinaryUpload cloudinaryUpload = CloudinaryUpload();
If that is supposed to creating new instance it should be
private CloudinaryUpload cloudinaryUpload = new CloudinaryUpload();
Note ... new. If you leave out the new, then that is a call to a method named CloudinaryUpload ... and no such method exists in your code.

Not able to parse data to a class in Java [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to learn object oriented code in Java, and am following a tutorial. I am currently stuck trying to parse a string into my class. It is returning the following error:
Name cannot be resolved to a variable
I have a main file, called start.java, and the class I am trying to call is in a different file, called phone.java. Both are in a folder called src. Below is the start.java code (which is throwing the error)
package src;
public class Start {
public static void main(String[] args){
phone android = new phone(Name:"android 10");
System.out.println(android.getName());
}
}
And here is the class I am trying to call, in phone.java
package src;
public class phone{
private String name;
public phone(String name) {
this.name = name;
}
public String getName(){
return this.name;
}
}
Much thanks for your help
You need to remove the Name from new phone(Name:"android 10") and need to use new phone("android 10").
You just need to pass the value for the name, your constructor will bind it to the name variable.
Refer below code
public class Start {
public static void main(String[] args){
phone android = new phone("android 10");
System.out.println(android.getName());
}
}

Java singleton - null static variable (can't explain) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
Can someone please explain how is possible, that method obtain(..) throws IllegalStateException for input ConfiguratorType.SKODA (the variable configurators contains {SKODA=null})? How can it be null, I do not understand why SkodaConfigurator.INSTANCE returns null. It should never be null or am i mistaken? The code is executed in servlet environment, Java 7.
Thank you
public class CarConfigurators {
private static Map<ConfiguratorType, CarConfigurator> configurators
= new EnumMap<ConfiguratorType, CarConfigurator>(ConfiguratorType.class);
static {
configurators.put(ConfiguratorType.SKODA, SkodaConfigurator.INSTANCE);
// ..
}
public static CarConfigurator obtain(ConfiguratorType type) {
CarConfigurator configurator = configurators.get(type);
if (configurator == null)
throw new IllegalStateException("Car configurator of type " + type + " is not registered.");
return configurator;
}
...
}
public class SkodaConfigurator extends CarConfigurator {
public static final SkodaConfigurator INSTANCE = new SkodaConfigurator();
...
}
public enum ConfiguratorType {
SKODA,
// ..
}
Static code cannot all run simultaneously, the various bits of static initialization going on have to happen in a given order. Clearly in this case, your static block which does configurations.put(...) is running before the static variable in SkodaConfiguration is initialized.
This is related to static initialization order.
I found this from another answer
public class Main {
{
System.out.printf("NON-STATIC BLOCK\n");
}
static{
System.out.printf("STATIC BLOCK\n");
}
public static Main m = new Main();
public Main(){
System.out.printf("MAIN CONSTRUCTOR\n");
}
public static void main(String... args) {
//Main m = new Main();
System.out.printf("MAIN METHOD\n");
}
}
Output :
STATIC BLOCK
NON-STATIC BLOCK
MAIN CONSTRUCTOR
MAIN METHOD
Please go through this : Java Static Initialization Order

Getting Compile time error in java funciton body [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I have created below code in java but getting error during compilation i have gone through with complete code but not able debug the problem
package csaAutomation;
import com.thoughtworks.selenium.SeleneseTestCase;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.util.Date;
import java.util.Date.*;
import java.text.SimpleDateFormat;
public class GuiAutomation extends SeleneseTestCase {
#BeforeTest
public void setUp() throws Exception {
WebDriver driver = new FirefoxDriver();
String baseUrl = "url";
selenium = new WebDriverBackedSelenium(driver, baseUrl);
}
#Test
public void testGUI_Automation() throws Exception {
String VAR_1="30000";
public void GUI_Login_MR_SIT() throws Exception {
selenium.open("/CSAlogin");
selenium.type("//input[#id=\"username\"]", "Administrator");
selenium.type("//input[#id=\"password\"]", "Ari_123");
selenium.click("//img[#src=\"/csaweb/resources/images/buttons/bf_login.gif\"]");
System.out.println("-----------GUI Login Successful-----------");
}
}
#AfterTest
public void tearDown() throws Exception {
selenium.stop();
}
}
Below is the error message :
C:\CSA_GUI_Automation_0.4\src\csaAutomation\GuiAutomation.java:34: illegal start
of expression
public void GUI_Login_MR_SIT();
^
Please assist
You've declared a method inside another method. I'm not sure of the intent of your code but I suspect it will work better if you have:
#Test
public void testGUI_Automation() throws Exception {
String VAR_1="30000";
GUI_Login_MR_SIT();
}
public void GUI_Login_MR_SIT() throws Exception {
selenium.open("/CSAlogin");
selenium.type("//input[#id=\"username\"]", "Administrator");
selenium.type("//input[#id=\"password\"]", "Ari_123");
selenium.click("//img[#src=\"/csaweb/resources/images/buttons/bf_login.gif\"]");
System.out.println("-----------GUI Login Successful-----------");
}
}
public void testGUI_Automation() throws Exception {
String VAR_1="30000";
public void GUI_Login_MR_SIT();
You are trying to declare a method within a method body. This syntax:
is unsupported in Java;
generally has no apparent meaning which could be assigned to it.

No suitable driver found for jdbc:mysql://localhost/db [duplicate]

This question already has answers here:
Better understaning - Class.forName("com.mysql.jdbc.Driver").newInstance ();
(4 answers)
Closed 10 years ago.
I am a java beginner and trying to insert a row in db. This is first time in java i am performing insertion operation. For around 2 Hrs i was googling and frustated and cannot solve my error. I called my friend and he gave live support for me in team viewer and added just one line of code to my program.
Class.forName("com.mysql.jdbc.Driver")
Can anyone please explain why we need to include this in my code before connection string. Is it necessary to place my code there each and every time. Please Explain me in Detail.
Here is some very simplified code that illustrates how driver initialization works. There are 3 classes, please put each one in an appropriately-named file.
import java.util.HashMap;
import java.util.Map;
public class DriverMgr {
private static final Map<String, Class<?>> DRIVER_MAP = new HashMap<String, Class<?>>();
public static void registerDriver(final String name, final Class<?> cls) {
DRIVER_MAP.put(name, cls);
}
public static Object getDriver(final String name) {
final Class<?> cls = DRIVER_MAP.get(name);
if (cls == null) {
throw new RuntimeException("Driver for " + name + " not found");
}
try {
return cls.newInstance();
} catch (Exception e) {
throw new RuntimeException("Driver instantiation failed", e);
}
}
}
public class MysqlDriver {
static {
// hello, I am a static initializer
DriverMgr.registerDriver("mysql", MysqlDriver.class);
}
#Override
public String toString() {
return "I am the mysql driver";
}
}
public class TestProg {
public static void main(final String... args) {
try {
Class.forName("MysqlDriver"); // try with or without this
} catch (Exception e) {
throw new RuntimeException("Oops, failed to initialize the driver");
}
System.out.println(DriverMgr.getDriver("mysql"));
}
}
When you call Class.forName, the driver class gets loaded and the static initializer gets executed. That in turn registers the driver class with the driver manager, so that the manager is now aware of it. Obviously, this only needs to be done once.
Class.forName("com.mysql.jdbc.Driver")
It must be required to load the driver of database which you are using.
the forNmae() method in this line load the driver of mysql database.
Yes , it's necessary to include every time .
but you can use a method for not repeating the codes .
for example
public void connectToMYSQL(){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/yourdatabase","username",""password);
}catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
and before writing any sql statement , just call the method for example
public void insert(String sql)
{
connectToMYSQL();
//.. then do your stuffs
}
The basic idea is that this action forces the driver class to be registered in JDBC's driver manager.
The method Class.forName("fully qualified class name) is used to initialize the static fields of the class and load the JDBC driver class, MySQL driver in your case, to your application. When it is instantiated, it gets registered with the DriverManager. By the latter you create connections, using Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/databasename","login","password");, which you later use to query the database.

Categories