I'm just facing problem when i m trying to call MainActivity method on my fragment activity. please let me know how can i fix it.
here is the method code snippet inside - MainActivity.java
-> this code is just for retrieving name column from database so that I could load on my spinner which is in a FragmentActivity.java.
public List<String> getAllLedgerName(){
List<String> names= new ArrayList<String>();
//select All Query
String Selectquery = "Select name from Ledger_info";
SQLiteDatabase dbname = getReadableDatabase();
Cursor cursorname = dbname.rawQuery(Selectquery, null);
if(cursorname.moveToFirst())
{
do{
names.add(cursorname.getString(0));
}while (cursorname.moveToNext());
}
return names;
}
-> here is the code of FragmentActivity.java class where i want to call above method to load retrieved data on spinner.
private void loadSpinnerData() {
ControllerActivites ca = (ControllerActivites)getActivity();
List<String> namelist = ca.getAllLedgerName();
//create adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item,namelist);
//list view with drop down
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//attaching data adapter to spinner
spin_Sales_Party.setAdapter(dataAdapter);
}
Its not loading the data rather it crashes my app when I do perform this action.
Whatever I have written initializing and method calling code I think that's not correct please give me solution or if you think is there any error in rest of code then please let me know. thanks.
Related
I have an ASP.NET service. Inside it I use the access to SQL Server, i.e. add the DbConnection etc
services.AddScoped<IDbConnection, SqlConnection>(_ => new SqlConnection(sqlcon));
services.AddScoped<ICRepository, CRepository>();
This DbConnection used in the classes which queried a db.
BUT I can execute the query 1st time and it returns the correct items.
If I execute it the 2nd time I receive:
"message": "The ConnectionString property has not been initialized."
and really the dbconnection object is empty and not initialized
How to fix it?
That is minimized piece of code
public class CRepository : ICRepository
{
private readonly IDbConnection _db;
public CRepository(IDbConnection db)
{
_db = db;
}
//....
public List<MyAction> GetMyActions()
{
var sql = $#"
SELECT id,name, description from actions;";
var result = _db.Query<MyAction>(sql).ToList();
return result;
}
...
}
ANd the first time the _db is initialized but the 2nd one - no.
In JAVA FX have an application with one window that creates a customer with insurances and another window with a tableView where I want to show information about the customers. In the tableView window, I also have an ArrayList. When I close the registration window the application sends the customer object to the ArrayList. This works fine, but when I do register another customers insurance, the ArrayList seems to go empty before taking in the new object. Summarized it seems that my ArrayList only will hold one object at a time.
//In the registration controller, this code is called when I close the window and pass the customer object
FXMLDocumentController controller = loader.getController();
controller.initForsikredeKunder(passedCustomer);
//---------- In the view tableclass
private ArrayList = null;
public void initForsikredeKunder (Kunde customer) {
if(kundeListe == null) {
kundeListe = new ArrayList<Kunde>();
}
this.kundeListe.add(customer);
}
Why does the ArrayList just hold one customer? seems to me that this code only makes one ArrayList, and then just should just add customers
as they get passed to the method. However, that is not happening
You do appear to have a typo, so I assume private ArrayList = null is really:
private ArrayList kundeListe = null;
The code looks fine (I'm guessing at some of the context), although there are some things I would improve. It only creates a new list when "kundeListe" is null - so the list isn't getting wiped out. So if you call initForsikredeKunder() a second time, all it'll do is add a second "customer".
Basically, you can just call initForsikredeKunder() repeatedly and it'll work fine.
I would rename initForsikredeKunder to say "add" instead of init. It's really an add operation which also handles lazy initialization of a backing list.
Going further, you could do this:
private List<Kunde> kundeListe = new ArrayList<>();
and remove the lazy init:
public void addKunder (Kunde customer) {
kundeListe.add(customer);
}
Note: I'm not 100% I understood your narrative above, so I could be misunderstanding what's going on. If this "dialog/window" only ever works with a single customer, you don't even need to use the List!
Edits after additional info provided:
Based on your code it looks like the original dialog isn't being re-used. The "new FXMLLoader()" part is fine.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("FXMLDocument.fxml"));
Parent tableViewParent = loader.load();
Scene tableViewScene = new Scene(tableViewParent); //Access the controller and call the method
FXMLDocumentController controller = loader.getController();
controller.initForsikredeKunder(valgtKunde); // having trouble finding what it should say instead of = new FXMLLoader();
So probably the easiest thing to do, if your dialog requires multiple customers, is to just pass in more than one with the initForsikredKunder() call.
How about this?
public void initForsikredeKunder (Kunde... customer) {
if(kundeListe == null) {
kundeListe = new ArrayList<Kunde>();
}
for (Kunde cust : customer) {
this.kundeListe.add(cust);
}
}
Then change the initForsikredeKunder() call to this:
controller.initForsikredeKunder(valgtKunde1, valgtKunde2, valgtKunde3);//Add as many as you need
If you already have a long list of "valgtKunde":
public void initForsikredeKunder (List<Kunde> customers) {
if(kundeListe == null) {
kundeListe = new ArrayList<Kunde>();
}
this.kundeListe.addAll(customers);
}
... and pass the list to initForsikredeKunder(customerList);
This is the kind of thing where the larger context matters, unfortunately it's hard to convey all of that here I guess, so there could be some adjustments required depending on broader context. (i.e. what data you're starting with and what the dialog functionality supports)
My Controller code
ParaEntities db = new ParaEntities();
public List<Client> GetAllClients()
{
return db.Client.ToList();
}
Please click this link to see the error message
It is weird that when I am first time to click the button to get all client information then it responses 500. In the second time, I click the button to get all client, which is success.
You should assign variable and display the data in View.
Please change the syntax as i write below.
ParaEntities db = new ParaEntities();
public List<Client> GetAllClients()
{
var getData= db.Client.ToList();
if(getData==null)
{
return null;
}
return getData;
}
This error points to a connection problem rather then code issue. Check that the connectionstring is valid and that the user specified in the connectionstring has access to the database. If you're running the application on IIS then make sure that the applicationpool user has access to the database. Here is another SO issue were they solved this error.
If you want to store the db context as a local variable in your controller class then I suggest you to instantiate it inside of the controllers constructor. Then you make sure that every time a instance of the controller is created then a new db context is created as well.
Lets say your controller namned ClientController
private ParaEntities db;
public ClientController()
{
this.db = new ParaEntities();
}
public List<Client> GetAllClients()
{
return db.Client.ToList();
}
Another approach is to wrap your db context in a using statment inside of your method. In that case you make sure that the method is using a fresh context when being called upon and that the context is being disposed when the operation is completed.
public List<Client> GetAllClients()
{
using(ParaEntities db = new ParaEntities())
{
return db.Client.ToList();
}
}
PS: both examples violates the dependency inversion principle (hard coupling to the db context) but thats for another day
Please try this
public List<Client> GetAllClients()
{
ParaEntities db = new ParaEntities();
return db.Client.ToList();
}
Here is the code that set the user data.
FXMLLoader loader = new FXMLLoader(getClass().getResource(page.toString()));
Parent node = (Parent) loader.load();
loader.getController().setParent(handler);
controllers.put(page.name(), loader.getController());
controller = controllers.get(page.name());
node.setUserData(page.name());
node.getProperties().put("page", page.name());
the variable page is an Enum object, I'm using the name 'page.name()' as the user data
Here is the code that gets the user data:
while (iterator.hasNext()) {
Node node = iterator.next();
String name = node.getProperties().get("page").toString();
System.out.println(name);
....
}
now this what happens:
the System.out.println(name); prints out the user data correctly,
and then I get a NullPointException pointing to
String name = node.getProperties().get("page").toString();
the point is System.out.println(name); that prints the user date
prints it correct.
What's going on here?
Try breaking it down. Code is usually easier to read when you do not chain multiple method calls together.
Node node = iterator.next();
ObservableMap<Object, Object> properties = node.getProperties(); // Swap Object with whatever subclass it is supposed to return
Object nameProperty = properties.get("page"); // Same here
String name = nameProperty.toString();
Then see which line returns the null pointer exception and try searching for the reason behind it. It is likely that either getProperties() or get("page") is in fact returning a null value.
I'm having a bit of trouble with knowing how I can save items from a list I have in Application state in the global.asax as- Application[""].
My controller basically takes in some user input, I then pass this to another classes Method as parameters it gets added to a list all the time. This data thats getting added to the list I want to store it, but without using a db. By using Application State. . I have been instantiating this class and calling its method to return the list of items but I dont think Application State is saving it.
Here is what I have so far. .
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
TimeLineInformation t = new TimeLineInformation();
IList<SWTimeEnvInfoDTO> g = t.getInfo();
Application["AppID"] = g;
}
^ Global.asax
IList<SWTimeEnvInfoDTO> result = new List<SWTimeEnvInfoDTO>();
public void returnTimeLineInfo(string SWrelease, string EnvName, DateTime SDate, DateTime EDate) {
SWTimeEnvInfoDTO myDTO = new SWTimeEnvInfoDTO();
myDTO.softwareReleaseName = SWrelease;
myDTO.environmentName = EnvName;
myDTO.StartDate = SDate;
myDTO.EndDate = EDate;
result.Add(myDTO);
getInfo();
}
public IList<SWTimeEnvInfoDTO> getInfo() {
return result;
}
^ class im calling
The SWTimeEnvInfoDTO type has get and set methods for the data.
I am calling the application from a View as well. It works with a string
Application["AppID"] = "fgt";
and shows this once i read it from my view.
Updated: You should use casting to retrieve variable you have saved at Application, and save it if it has changed:
IList<SWTimeEnvInfoDTO> result = Application["AppID"] != null ? (IList<SWTimeEnvInfoDTO>)Application["AppID"] : new List<SWTimeEnvInfoDTO>(); // retrieve data
result.Add(...); // add some new items
Application["AppID"] = result; // save added values