Accessing variable from other class returns null - arrays

I did a separate levelData class to be able to flexibly add levels. I was happy with it until my supervisor ordered me to convert my levelData into XML. I did an XML version of the levelData's data (question, answers, correct answer...). I used the old class and converted it so that it fetches the XML.
All seems well, I did traces of my answers array and it printed nicely...
But the headache started when I tried this.
// This code appears in a different class with
// currentLvl:LevelData initialized in the constructor.
quizHolder.ansA.ansHud.text = currentLvl.choices[1];
quizHolder.ansB.ansHud.text = currentLvl.choices[2];
quizHolder.ansC.ansHud.text = currentLvl.choices[3];
quizHolder.ansD.ansHud.text = currentLvl.choices[4];
// BTW, I can't make a for loop to do the same function as above. So wierd.
I tried to run it. it returned:
TypeError: Error #2007: Parameter text must be non-null.
at flash.text::TextField/set text()
at QuestionPane/setQuiz()
at QuestionPane/setQuestion()
at QuestionPane()
at LearningModule()
Where did I go wrong? I tried making a custom get function for it, only to get the same error. Thanks in advance. If I need to post more of the code, I will gladly do so =)
LevelData Class in PasteBin: http://pastebin.com/aTKC1sBC

Without seeing more of the code it's hard to diagnose, but did you correctly initialize the choices Array before using it? Failing that I think you'll need to post more code.
Another possible issue is the delay in loading the XML data. Make sure your data is set before QuestionPane tries to access it.

When did you call
quizHolder.ansA.ansHud.text = currentLvl.choices[1];
quizHolder.ansB.ansHud.text = currentLvl.choices[2];
quizHolder.ansC.ansHud.text = currentLvl.choices[3];
quizHolder.ansD.ansHud.text = currentLvl.choices[4];
these? You load the XML and on complete you fill the array, what is correct. but is the XML loaded and parsed to the time when you access (fill the TextFields) the choices array already?

Related

FireStore and maps/arrays, document-list to array in Kotlin

I've finally started to understand a lot of info regarding FireStore, but I'm wondering if I can get some assistance.
If I had a setup similar to or like this:
          races
                Android
                      name: Android
                      size: medium
                       stats          <---- this is the map
                                str: 10
                                sex: 12.... (more values)
How would I parse this? I am looking to make specific TextViews apply values found in the database so that I can simply update the database and my app will populate those values so that hard coding and code updating won't be nearly as troublesome in the future.
I currently use something like this:
val androidRef = db.collection("races").document("Android")
androidRef.get().addOnSuccessListener { document ->
if (document != null) {
oneOfTheTextViews.text = document.getString("str")
} else {
}
The issue is currently I can only seem to access from collection (races) / document (android) / then a single field (I have "str" set as a single field, not part of a map or array)
What would the best practice be to do this? Should I not nest them at all? And if I can reference said nesting/mapping/array, what functions need to be called? (To be clear, I am not asking only whether or not it is possible - the reference guides and documents allude to such - but what property/class/method/etc needs to be called in order to access only one of those values or point to one of those values?).
Second question: Is there a way to get a list of document names? If I have several races, and simply want to make a spinner or recycler view based on document names as part of a collection, can I read that to the app?
What would the best practice be to do this?
If you want to get the value of your str property which is nested within your stats map, please change the following line of code:
oneOfTheTextViews.text = document.getString("str")
to
oneOfTheTextViews.text = document.getString("stats.str")
If your str property is a number and not a String, then instead of the above line of code please use this one:
oneOfTheTextViews.text = document.getLong("stats.str")
Should I not nest them at all?
No, you can nest as many properties as you want within a Map.
Is there a way to get a list of document names?
Yes, simply iterate the collection and get the document ids using getId() function.

peewee get_or_create and then save: error binding

Is there an easy way to update a field on a get of a get_or_create?
I have a class ItemCategory and I want to either create a new entry or get the already created entry and update a field (update_date).
What I do is:
item,created= ItemCategory.get_or_create(cat=cat_id,item=item_id)
if created == True:
print "created"
else:
item.update_date = datetime.now
item.somethingelse = 'aaa'
item.save()
This works for a while in my loop. But after 50-100 get/create it crashes:
peewee.InterfaceError: Error binding parameter 4 - probably unsupported type.
Maybe I should use upsert(), I tried but wasn't able to get anything working. Also it's not probably the best solution, since it makes a replace of the whole row instead of just a field.
I like peewee, it's very easy and fast to use, but I can't find many full examples and that's a pity
Newbie mistake
item.update_date = datetime.now()
I am not 100% sure this is the only answer though. I modified my code so many times that it might be also something else.
Regarding my question about create_or_update , I've done this:
try:
Item.create(...)
except IntegrityError:
Item.update(...)
peewee is really great, I wonder why no one ever asked for a create_or_update.

BadValueError: Expected GeoPt

I am trying to pass values from a html form into the gae datastore and receive the following error:
"BadValueError: Expected GeoPt, got (51.123, -0.123)"
Apologies up front, but I'm new to this so may be going about it all wrong but would really appreciate some advice on the best way of doing this.
Thanks in advance.
Looks like you're trying to assign a tuple to the property instead of a geopt.
Your code does this:
entity.some_geo_property = (51.123, -0.123)
Where it needs to do this:
from google.appengine.ext import ndb
entity.some_geo_property = ndb.GeoPt(51.123, -0.123)
alternatively using the tuple:
entity.some_geo_property = ndb.GeoPt(*value)
Without seeing your exact code it's hard to offer a complete solution. If you provide code I'll try to update my answer.
I have never worked with GeoPt properties, but I would think that you passed a tuple of two floats.
Pass the value to ndb.GeoPt() first and store the return value in NDB. Example:
location = ndb.GeoPt(51.123, -0.123)
If you use the DB API of HRD rathern than the NDB API, use db.GeoPt() which is basically the same.

How to access a main document class array from a movieclip?

I have an array in my main
public var graphArray:Array = [1,2,3,4,5,6];
And I'm trying to access it from within a MovieClip that I've put on my timeline using:
var graph1scale:Number = MovieClip(root).graphArray[0]
It looks like it would make sense to me but when I try to run it I get this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
Am I wrong to be using MovieClip(root) to try and access it? I've only just started using external classes (this is my first project doing so) and usually I just do everything on the timeline. So MovieClip(root) is familiar to me but I guess it's not the right thing to do here.
Is there a way I can access vars from Main.as?
-----SOLVED-----
I realised MovieClip(root) did work all along but I was just calling on the array before the array was being defined in Main.as. I put a delay on calling graphArray and it worked.
Not sure how that makes sense though because the graphArray is the first thing I've defined in the whole main.as class
Try using this instead
MovieClip(this.root)
This works for me on a test that you can see here:
http://marksost.com/test/as3arrayaccess/
And the source files here:
http://marksost.com/test/as3arrayaccess/test.zip

Flex 4 converting array.length into a string

so I am trying to assign a number to a variable that is dynically generated from a binded array...when i try and assign it and trace it out nothing happens, which means I am obviously doing something wrong but I am not sure? just for fun i decided to bind the data to a label like so...
<s:Label text="{this.dd.selectedViews.length}"/>
and that work and updated properly, but when running in debug mode i got this warning...
warning: unable to bind to property 'length' on class 'Array' (class is not an IEventDispatcher)
so what would be the best method of assigning the array to a variable to use throughout my application
thanks in advance for any help
I'm not really sure what you are asking here, but maybe this will help you out. As your error message says, the Array class is not an IEventDispatcher. What that means is that if you try to use a plain old Array as the source of a data-binding expression, it generally is not going to work.
If you need to bind to an array, you can try using a different class such as ArrayCollection, which supports data binding.

Resources